Skip to content

XPathQueriesinC#

Simon Jackson edited this page Jun 7, 2017 · 1 revision

XPath Queries in C#

Here is a simple tutorial on using XPath Queries in C#:

First lets create an XmlDocument object so we can load an XML File:

XmlDocument xmldoc = new XmlDocument();

Now we can load the xml file with the XmlDocument.Load() function

xmldoc.Load("ZiggySaverConfig.xml");

Create a stream reader to extract the InnerXML from the XmlDocument we just loaded:

System.IO.StringReader sr = new System.IO.StringReader(xmldoc.InnerXml);

With this StreamReader object, we can create an XPathDocument object so we can run XPath queries

XPathDocument doc = new XPathDocument(sr);

Now we need to create an XPathNavigator object that will allow us to compile an XPath query and select values from the XmlDocument :)

XPathNavigator nav = doc.CreateNavigator(); 

The Compile method of the XPathNavigator will increase the speed at which XPath data selections can occur in the XmlDocument

XPathExpression expImgSize = nav.Compile(@"/ZiggySaverConfig/@ImageSize");

Call the Select() method of the XPathNavigator to retrieve an interator container of XPathNode elements that were returned from the XPath query

XPathNodeIterator iterImageSize = nav.Select(expImgSize);

Dont forget to check to see if the iterator is null, this would indicate an empty return set from the XPath query.

if(iterImageSize != null)
{

The XpathNodeIterator is traversed by calling the MoveNext() method. You must call this method before trying to retrieve any data from the iterator:

    while(iterImageSize.MoveNext())
    {

Get the value of the XPath query result by using the iterator's Current.Value property

        imageSize = iterImageSize.Current.Value;
    }
}
Clone this wiki locally