Get a chunk of the Xml File using Xpath classes




The following code get a portion of a Xml file using XpathNavigator and XpathExpression Classes. XpathNodeIterator is used to read the nodes returned by the Xpath query.

In the following Xml file(books.xml) used in this example, XpathExpression is applied to get only the information for the first book






Everyday Italian
Giada De Laurentiis
2005
30.00


Harry Potter
J K. Rowling
2005
29.99







System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
xdoc.Load("d:\\books.xml");
System.Xml.XPath.XPathNavigator xp = xdoc.CreateNavigator();
System.Xml.XPath.XPathExpression xpe = xp.Compile("/bookstore/book[1]");
System.Xml.XPath.XPathNodeIterator xni = xp.Select(xpe);
while (xni.MoveNext())
Console.WriteLine(xni.Current.OuterXml);


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: