July 01, 2003
Parsing XML Files in .NET Using C#
Parsing XML Files in .NET Using C#
Listing 5 Parsing XML using XPathDocument
using System;
using System.Xml.XPath;
using CommonLib;
namespace Run
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
CommonLib.Suite s = new CommonLib.Suite();
XPathDocument xpd = new XPathDocument("..\\..\\..\\..\\testCases.xml");
XPathNavigator xpn = xpd.CreateNavigator();
XPathNodeIterator xpi = xpn.Select("/suite/testcase");
while (xpi.MoveNext()) // each testcase node
{
CommonLib.TestCase tc = new CommonLib.TestCase();
tc.id = xpi.Current.GetAttribute("id", xpn.NamespaceURI);
tc.kind = xpi.Current.GetAttribute("kind", xpn.NamespaceURI);
XPathNodeIterator tcChild = xpi.Current.SelectChildren(XPathNodeType.Element);
while (tcChild.MoveNext()) // each part (<inputs> and <expected>) of <testcase>
{
if (tcChild.Current.Name == "inputs")
{
XPathNodeIterator tcSubChild = tcChild.Current.SelectChildren(XPathNodeType.Element);
while (tcSubChild.MoveNext()) // each part (<arg1>, <arg2>) of <inputs>
{
if (tcSubChild.Current.Name == "arg1")
tc.arg1 = tcSubChild.Current.Value;
else if (tcSubChild.Current.Name == "arg2")
tc.arg2 = tcSubChild.Current.Value;
}
}
else if (tcChild.Current.Name == "expected")
tc.expected = tcChild.Current.Value;
}
s.items.Add(tc);
} // each testcase node
s.Display();
} // Main()
} // class Class1
} // ns Run
Previous Page |
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
10
|
11
|
12
|
13
|
14
|
15
|
16
Next Page