Querying in an XML file
Hi,
I have the XML file shown below.
Now I am trying to retrieve all the 'Event' nodes using XPath. The
code being used is shown below.
The output being ouput is the data in the SubNodes given as one string.
(Output shown below)
Is there a way how to iterate through the sub-nodes(From, To,
Lookahaed). What I was doing is three different XPathExpressions for
each sub-node, but I think thats not very effecient.
Can someone help me out
Thanks in Advance
/* code */
StreamReader reader = new StreamReader(filePath, Encoding.UTF8);
XPathDo***ent doc = new XPathDo***ent(reader);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression xpe = nav.Compile("Simulation/Events/Event");
XPathNodeIterator xpni = nav.Select(xpe);
while(xpni.MoveNext())
{
Console.WriteLine(xpni.Current.Value.ToString());
}
/* Output */
S1S21
S3S11
S2S31
S3S21
/* Sample of xml file */
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Simulation>
<States>
<State>S1</State>
<State>S2</State>
<State>S3</State>
</States>
<Events>
<Event>
<From>S1</From>
<To>S2</To>
<Lookahaed>1</Lookahaed>
</Event>
<Event>
<From>S3</From>
<To>S1</To>
<Lookahaed>1</Lookahaed>
</Event>
<Event>
<From>S2</From>
<To>S3</To>
<Lookahaed>1</Lookahaed>
</Event>
<Event>
<From>S3</From>
<To>S2</To>
<Lookahaed>1</Lookahaed>
</Event>
</Events>
<Instances />
</Simulation>
|