XPathExpression对象表示由Compile类的静态XPathExpression方法或Compile类的XPathNavigator方法返回的已编译的XPath查询。
XPathExpression 类
如果多次使用同一 XPathExpression XPath 查询,则由对象表示的已编译 XPath 查询非常有用。
例如,在多次调用Select方法时,不要每次都使用表示 XPath 查询的字符串,而应使用Compile类的方法或XPathExpression类的方法来将 XPath 查询编译并缓存到Compile对象中,以便于重复使用和提高性能。
编译后, XPathExpression 对象可以用作以下 XPathNavigator 类方法的输入,具体取决于从 XPath 查询返回的类型。
下表描述了每个 W3C XPath 返回类型、其与 Microsoft .NET Framework 的等价性,以及根据对象 XPathExpression 的返回类型可使用的方法。
| W3C XPath 返回类型 | .NET Framework 等效类型 | DESCRIPTION | 方法 |
|---|---|---|---|
Node set |
XPathNodeIterator | 未排序的节点集合,按照文档顺序创建,没有重复。 | Select 或 Evaluate |
Boolean |
Boolean |
true 值或 false 值。 |
Evaluate 或 Matches |
Number |
Double | 浮点数。 | Evaluate |
String |
String | UCS 字符序列。 | Evaluate |
注释
该方法 Matches 接受 XPath 表达式作为其参数。 该方法 SelectSingleNode 返回对象 XPathNavigator ,而不是 W3C XPath 返回类型之一。
ReturnType 属性
将 XPath 查询编译为对象 XPathExpression 后,可以使用 ReturnType 该对象的属性 XPathExpression 来确定 XPath 查询返回的内容。
该 ReturnType 属性返回表示 W3C XPath 返回类型的以下 XPathResultType 枚举值之一。
以下示例使用 XPathExpression 对象从 books.xml 文件返回数字和节点集。
ReturnType每个XPathExpression对象的属性以及方法EvaluateSelect的结果将写入控制台。
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);
该示例将 books.xml 文件作为输入。
<?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>
高性能 XPath 表达式
为了获得更好的性能,请在查询中使用最具体的 XPath 表达式。 例如,如果 book 节点是节点的 bookstore 子节点,并且 bookstore 节点是 XML 文档中最顶层的元素,则使用 XPath 表达式 /bookstore/book 的速度比使用 //book快。
//book XPath 表达式将扫描 XML 树中的每个节点,以标识匹配的节点。
此外,使用XPathNavigator类提供的节点集导航方法,在选择标准简单的情况下,可能会比XPathNavigator类提供的选择方法提高性能。 例如,如果需要选择当前节点的第一个子级,则使用 MoveToFirst 该方法的速度比使用 child::*[1] XPath 表达式和 Select 方法要快。
有关类的 XPathNavigator 节点集导航方法的详细信息,请参阅 使用 XPathNavigator 的节点集导航。