已编译的 XPath 表达式

更新:November 2007

XPathExpression 对象表示从 XPathExpression 类的静态 Compile 方法或 XPathNavigator 类的 Compile 方法返回的已编译 XPath 查询。

XPathExpression 类

如果多次使用相同的 XPath 查询,通过 XPathExpression 对象表示的已编译 XPath 查询非常有用。

例如,如果多次调用 Select 方法,而不是每次使用表示 XPath 查询的字符串,请使用 XPathExpression 类的 Compile 方法或 XPathNavigator 类的 Compile 方法来编译并缓存 XPathExpression 对象中的 XPath 查询,以便重复使用并提高性能。

编译后,根据从 XPath 查询返回的类型,XPathExpression 对象也许可以作为下列 XPathNavigator 类方法的输入。

下表介绍每种 W3C XPath 返回类型、其等效的 Microsoft .NET Framework 类型以及 XPathExpression 对象基于其返回类型也许可以用于的方法。

W3C XPath 返回类型

等效的 .NET Framework 类型

说明

方法

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 对象之后,可以使用 XPathExpression 对象的 ReturnType 属性来确定 XPath 查询返回的内容。

ReturnType 属性返回表示 W3C XPath 返回类型的下列 XPathResultType 枚举值之一。

以下示例使用 XPathExpression 对象从 books.xml 文件中返回一个数字和一个节点集。 每个 XPathExpression 对象的 ReturnType 属性以及 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 文件作为输入。

<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 的节点集定位

请参见

概念

使用 XPath 数据模型处理 XML 数据

使用 XPathNavigator 选择 XML 数据

使用 XPathNavigator 计算 XPath 表达式

使用 XPathNavigator 匹配节点

XPath 查询识别的节点类型

XPath 查询和命名空间

参考

XmlDocument

XPathDocument

XPathNavigator