XPathExpression Class
.NET Framework 2.0
Provides a typed class that represents a compiled XPath expression.
Namespace: System.Xml.XPath
Assembly: System.Xml (in system.xml.dll)
Assembly: System.Xml (in system.xml.dll)
This class is returned as the result of the Compile method of the XPathNavigator class. It is a parameter to the Evaluate, Matches, Select, and SelectSingleNode methods of the XPathNavigator class.
Notes to Inheritors When you inherit from the XPathExpression class, you must override the following members:The following example shows how to use the XPath return type to determine how to process the XPath expression.
Imports System Imports System.Xml Imports System.Xml.XPath Public Class XPathExpressionExample Public Shared Sub Main() Dim document As XPathDocument = New XPathDocument("contosoBooks.xml") Dim navigator As XPathNavigator = document.CreateNavigator() Dim expression1 As XPathExpression = XPathExpression.Compile(".//bk:price/text()*10") ' Returns a number. Dim expression2 As XPathExpression = XPathExpression.Compile("bk:bookstore/bk:book/bk:price") ' Returns a nodeset. Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable) manager.AddNamespace("bk", "http://www.contoso.com/books") expression1.SetContext(manager) expression2.SetContext(manager) Evaluate(expression1, navigator) Evaluate(expression2, navigator) End Sub Public Shared Sub Evaluate(ByVal expression As XPathExpression, ByVal navigator As XPathNavigator) Select Case expression.ReturnType Case XPathResultType.Number Console.WriteLine(navigator.Evaluate(expression)) Exit Sub Case XPathResultType.NodeSet Dim nodes As XPathNodeIterator = navigator.Select(expression) While nodes.MoveNext() Console.WriteLine(nodes.Current.ToString()) End While Case XPathResultType.Boolean If CType(navigator.Evaluate(expression), Boolean) Then Console.WriteLine("True!") End If Case XPathResultType.String Console.WriteLine(navigator.Evaluate(expression)) End Select End Sub End Class
XPathDocument doc = new XPathDocument("books.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr1 = nav.Compile(".//price/text()*10");
// Returns a number.
XPathExpression expr2 = nav.Compile("bookstore/book/price");
// Returns a nodeset.
Evaluate(expr1, nav);
Evaluate(expr2, nav);
} //main
public static void Evaluate(XPathExpression expr, XPathNavigator nav)
{
switch (expr.get_ReturnType()) {
case XPathResultType.Number:
Console.WriteLine(nav.Evaluate(expr));
break;
case XPathResultType.NodeSet:
XPathNodeIterator i = nav.Select(expr);
while (i.MoveNext()) {
Console.WriteLine(i.get_Current().ToString());
}
break;
case XPathResultType.Boolean:
if (System.Convert.ToBoolean(nav.Evaluate(expr))) {
Console.WriteLine("True!");
}
break;
case XPathResultType.String:
Console.WriteLine(nav.Evaluate(expr));
break;
}
} //Evaluate
The example takes the contosoBooks.xml file as input.
<bookstore xmlns="http://www.contoso.com/books">
<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>
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Community Additions
ADD
Show: