Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Querying in an XML file
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 20th April 10:56
curious
External User
 
Posts: 1
Default 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>
  Reply With Quote


  sponsored links


2 20th April 14:36
cerebrus
External User
 
Posts: 1
Default Querying in an XML file


Hi Curious,

Why not try using 2 XPathNavigators, one for the Event nodes and the
other for the children of the Event nodes ? See sample code below :

=============================
Private Sub Iteration()
Dim doc As XPathDo***ent = New XPathDo***ent("..\temp.xml")
Dim nav As XPathNavigator = doc.CreateNavigator()
Dim xpe As XPathExpression = nav.Compile("/Simulation/Events/Event")
Dim xpni As XPathNodeIterator = nav.Select(xpe)
While xpni.MoveNext()
Console.WriteLine("-------------")
Console.WriteLine(xpni.Current.LocalName())
Console.WriteLine("-------------")
Dim xpniChildren As XPathNodeIterator = xpni.Current.SelectChildren &
_(XPathNodeType.Element)
While xpniChildren.MoveNext
Console.Write(xpniChildren.Current.LocalName() & " : ")
Console.WriteLine(xpniChildren.Current.Value())
End While
Console.WriteLine("")
End While

End Sub
=============================

Hope this helps,

Regards,

Cerebrus.
  Reply With Quote
3 20th April 14:36
curious
External User
 
Posts: 1
Default Querying in an XML file


Thanks, that was what I required
  Reply With Quote
4 20th April 14:36
cerebrus
External User
 
Posts: 1
Default Querying in an XML file


Glad to assist...

Regards,

Cerebrus.
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666