XPath and .NET

XPath and .NET

An XML Document is modeled as a tree as of nodes that contains a variety of different data types. Each data type can include elements, attributes, and even text. The XML Path Language (XPath) is an expression based language that is used to identify and select these different data types from an XML document. Each XPath expression is a pre-defined set of criteria that can include data type, name, values, and even relationships. XPath can be used as an independent query language or in conjunction with XSLT and XPointer. When an XPath expression is executed it returns one of the basic data types as shown in Table 1. Within this article we will look at the basics of XPath and how it can be used within the .NET Framework.

 

Table 1: When executed an XPath expression returns one of the following data types.

· node set

· Boolean

· number

· string

 

For example, an XPath query to “find ‘customer’ elements that have a ‘state’ attribute’ with the value ‘NH’ and that are contained in the ‘customers’ element at the document root” can be encapsulated into an XPath expression as “[/customers/customer[@state=’NH’]. One of the important types of XPath expressions is a location path. This type of expression selects a set of nodes relative to the context node. The result of evaluating location path is a node-set that contains the selected nodes. These simple types of expressions are the core part of an XSLT transformation, which associates the specific expression results with templates to create a new XML document. XPath expressions using this type of syntax can be performed directly against the XML Document Object Model (DOM).

Within the .NET Framework the System.XML.XPath namespace provides both an XPath parser and evaluation engine that supports the W3C XML Path Language Version 1.0 Recommendation. This namespace also exposes several other classes that can be used to execute and navigate the results of XPath queries.

 

· XPathDocument – A read only cache for processing XML document and streams. This class is optimized for XSLT processing and specifically for the XPath object model. The XPathDocument uses a DOM based processing model, but for performance and usability reason doesn’t implement the ability to modify or delete nodes contained in either the XML document or stream.

· XPathNavigator – Provides the required methods needed to execute XPath queries against XML data from any type of data store. Typically, this includes an XPathDocument or dataset. The XPathNavigator is instantiated by using the CreateNavigator method of an XPathDocument object. One of the most important parts of this function is the ability to use this class to compile frequently executed XPath expressions and generate a System.XMLPath.PathExpression object that can encapsulate a compiled query.

· XPathNodeIterator – A forward read only way to access data within an XMLPathNavigator. When using the Select method of an XPathNavigator object to execute an XPath query, an instance of the XPathNodeIterator is created. The XPathNodeIterator is then used to navigate the resultset generated by the XPath query.

· XPathExpression – Encapsulates a compiled XPath query expression that conforms to the W3C XPath Query Language specification. A call to the Compile method of an XPathNavigator objects returns the XPathExpression class. Many times you would use the XPathExpression to supply a precompiled XPath query expression in a call to the Select method of an XPathNavigator object.

· XPathException – The .NET Framework exception that is generated when an error occurs during the processing of an XPath query expression. Both the Select and Compile methods of the XPathNavigator class can raise an XPathException.

 

For example, if we were to take the following sample customer XML file that contains a list of customers names and addresses.

 

<?xml version="1.0" encoding="utf-8" ?>

<NewDataSet>

<customer>

<customername>Big Company</customername>

<customeraddress>123 Anywhere St</customeraddress>

<customercity>Bedford</customercity>

<customerstate>NH</customerstate>

</customer>

<customer>

<customername>My Company</customername>

<customeraddress>456 Down St</customeraddress>

<customercity>Boston</customercity>

<customerstate>MA</customerstate>

</customer>

</NewDataSet>

 

We could write the following code sample that would iterate through the list of customers based on an XPath expression when a form button is selected.

Private Sub BtnReadXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReadXML.Click

'load the XPathDocument class.

Dim xmldoc As New System.Xml.XPath.XPathDocument("../customer.xml")

'load the XPathNavigator class.

Dim nav As System.Xml.XPath.XPathNavigator = xmldoc.CreateNavigator()

'load the XPathIterator class.

Dim iterator As System.Xml.XPath.XPathNodeIterator = _ nav.Select("//customer/customername")

'Use the XPathIterator class to navigate through the generated
'resultset and then display the selected customername.

Do While iterator.MoveNext

MsgBox(iterator.Current.Value)

Loop

End Sub

 

In this article we have covered some of the basics of XPath and how it can be used within the .NET Framework. XPath is an extremely powerful and useful way of retrieving data from all types of XML documents and data sources.