Reading XML Using XPathDocument


XmlReader is great, don't get me wrong; but it is a forward-only, non-cached way of reading XML data. Sometimes, instead of reading XML in this way, you want to hold the XML Infoset in memory while you jump from one point in the document to another to query the information you are interested in using.

XPath (discussed heavily in Chapter 10) is a great way to query XML for what you want. For this reason, the class, XPathDocument, in the .NET Framework. XPathDocument stores the XML data in memory and allows you to jump to any point of the document using XPath queries.

Note that if you are interested in altering or writing to the XML document in any manner, then you want to use the .NET Framework class, XmlDocument to accomplish this task. XmlDocument allows for reading and writing of an XML document, while XPathDocument allows only reading. With that said, if you are interested in just reading from the XML document, then you want to use XPathDocument because it is easier to use and performs better than XmlDocument does.

For an example of reading an XML document using the XPathDocument object, again turn to the stock order XML file, MyXml.xml, and use XPath to query a list of stock symbols from the document. This is illustrated in Listing 15-18.

Listing 15-18: Querying XML using XPath and the XPathDocument object

image from book
      using System;      using System.Collections.Generic;      using System.Text;      using System.Xml;      using System.Xml.XPath;      using System.IO;      namespace XmlProject      {          class Program          {               static void Main(string[] args)              {                  try                  {                     FileStream myStockOrders = new                        FileStream("C:/MyXml.xml", FileMode.Open);                     XPathDocument myDocument = new XPathDocument(myStockOrders);                     XPathNavigator docNavigation = myDocument.CreateNavigator();                     foreach(XPathNavigator node in                         docNavigation.Select                         ("//MultiStockOrder/StockOrderMultiple/StockOrder/Symbol"))                     {                         Console.WriteLine(node.Value.ToString());                     }                     Console.WriteLine("Done");                     Console.ReadLine();                  }                  catch (System.Exception ex)                  {                     Console.Error.WriteLine(ex.ToString());                     Console.ReadLine();                  }              }          }      } 
image from book

To use the XPathDocument class, you should first import the System.Xml.XPath namespace into the application. From the example in Listing 15-18, you can see that the instantiation of the XPathDocument object is passed the FileStream object which holds the MyXml.xml file contents.

Then, using a foreach command, you are able to iterate through everything retrieved from the XPath query-//MultiStockOrder/StockOrderMultiple/StockOrder/Symbol. For each item found with this XPath query, the value of the element is printed to the screen of the console application giving you the results shown in Figure 15-12.

image from book
Figure 15-12




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net