Compiled XPath Expressions
An XPathExpression object represents a compiled XPath query returned from either the static Compile method of the XPathExpression class or the Compile method of the XPathNavigator class.
The XPathExpression Class
A compiled XPath query represented by an XPathExpression object is useful if the same XPath query is being used more than once.
For example, when calling the Select method multiple times, instead of using a string representing the XPath query each time, use the Compile method of the XPathExpression class or the Compile method of the XPathNavigator class to compile and cache the XPath query in an XPathExpression object for reuse and improved performance.
Once compiled, the XPathExpression object may be used as input to the following XPathNavigator class methods depending on the type returned from the XPath query.
The following table describes each of the W3C XPath return types, their Microsoft .NET Framework equivalencies, and what methods the XPathExpression object may be used with based on its return type.
W3C XPath Return Type | .NET Framework Equivalent Type | Description | Methods |
---|---|---|---|
Node set |
XPathNodeIterator | An unordered collection of nodes without duplicates created in document order. | Select or Evaluate |
Boolean |
Boolean | A true or false value. |
Evaluate or Matches |
Number |
Double | A floating-point number. | Evaluate |
String |
String | A sequence of UCS characters. | Evaluate |
Note
The Matches method accepts an XPath expression as its parameter. The SelectSingleNode method returns an XPathNavigator object, not one of the W3C XPath return types.
The ReturnType Property
After an XPath query has been compiled into an XPathExpression object, you can use the ReturnType property of the XPathExpression object to determine what the XPath query returns.
The ReturnType property returns one of the following XPathResultType enumeration values representing the W3C XPath return types.
The following example uses the XPathExpression object to return a number and a node set from the books.xml
file. The ReturnType property of each XPathExpression object as well as the results from the Evaluate and Select methods are written to the console.
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()
' Returns a number.
Dim query1 As XPathExpression = navigator.Compile("bookstore/book/price/text()*10")
Console.WriteLine(query1.ReturnType)
Dim number As Double = CType(navigator.Evaluate(query1), Double)
Console.WriteLine(number)
' Returns a node set.
Dim query2 As XPathExpression = navigator.Compile("bookstore/book/price")
Console.WriteLine(query2.ReturnType)
Dim nodes As XPathNodeIterator = navigator.Select(query2)
nodes.MoveNext()
Console.WriteLine(nodes.Current.Value)
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();
// Returns a number.
XPathExpression query1 = navigator.Compile("bookstore/book/price/text()*10");
Console.WriteLine(query1.ReturnType);
Double number = (Double)navigator.Evaluate(query1);
Console.WriteLine(number);
// Returns a node set.
XPathExpression query2 = navigator.Compile("bookstore/book/price");
Console.WriteLine(query2.ReturnType);
XPathNodeIterator nodes = navigator.Select(query2);
nodes.MoveNext();
Console.WriteLine(nodes.Current.Value);
The example takes the books.xml
file as input.
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
Higher Performance XPath Expressions
For better performance, use the most specific XPath expression possible in your queries. For example, if the book
node is a child node of the bookstore
node and the bookstore
node is the top-most element in an XML document, using the XPath expression /bookstore/book
is faster than using //book
. The //book
XPath expression will scan every node in the XML tree to identify matching nodes.
Additionally, using the node set navigation methods supplied by the XPathNavigator class may result in improved performance over the selection methods supplied by the XPathNavigator class in cases where your selection criteria are simple. For example, if you need to select the first child of the current node, it is faster to use the MoveToFirst method than to use the child::*[1]
XPath expression and the Select method.
For more information about the node set navigation methods of the XPathNavigator class, see Node Set Navigation Using XPathNavigator.