| re: help reading xml doc
You can get another navigator out of the iterator that is positioned on each
chapter, then you can do sub-queries from there, for example:
XPathDocument xmlDoc = new XPathDocument("..\\..\\books.xml");
XPathNavigator xmlNav = xmlDoc.CreateNavigator();
XPathExpression xmlExpr = xmlNav.Compile("root/book[@title='Some
Title']/chapter");
XPathNodeIterator xmlItor = xmlNav.Select(xmlExpr);
while (xmlItor.MoveNext()) {
XPathNavigator subnav = xmlItor.Current;
XPathNavigator title = subnav.SelectSingleNode("title");
if (title != null) {
Console.WriteLine("title="+title.Value);
}
}
The subnav object is a Clone() of the original XPathNavigator. The Clone()
method is very cheap and allows you to have as many navigators on the same
tree as you need.
"David" <David@discussions.microsoft.com> wrote in message
news:FE02B4AA-2C86-412F-AE52-BECD637D4FE0@microsoft.com...[color=blue]
> I'm new to XML, so please bear with me.
> I'm using XPathNavigator and XPathNodeIterator to return a set of xml
> nodes
> from a document that looks like this:
> <root>
> <book title="Some Title">
> <chapter>
> <title>Chapter 1</title>
> <starting_page>34</starting_page>
> <number_of_pages>15</number_of_pages>
> </chapter>
> <chapter>
> <title>Chapter 2</title>
> <starting_page>50</starting_page>
> <number_of_pages>15</number_of_pages>
> </chapter>
> </book>
> </root>
>
> My code:
> XPathDocument xmlDoc = new XPathDocument("books.xml");
> XPathNavigator xmlNav = xmlDoc.CreateNavigator();
> XPathExpression xmlExpr = xmlNav.Compile("root/book[@title='Some
> Title']/chapter");
> XPathNodeIterator xmlItor = xmlNav.Select(xmlExpr);
> xmlItor.MoveNext();
>
> At this point I have a collection of chapter nodes in my xmlItor, but how
> do
> I then get the data out of the title, starting_page, and number_of_pages
> elements? I would prefer to be able to refer to them by element name.
>[/color] |