XPathExpression 物件代表一個已編譯的 XPath 查詢,這個查詢是從 Compile 類別的靜態 XPathExpression 方法或 Compile 類別的 XPathNavigator 方法傳回的。
XPathExpression 類別
如果多次使用相同的 XPath 查詢,由 XPathExpression 物件代表的已編譯 XPath 查詢會很有用。
例如,在多次呼叫 Select 方法時,不要每次使用字串來表示 XPath 查詢,而是使用 Compile 類別的 XPathExpression 方法或 Compile 類別的 XPathNavigator 方法,將 XPath 查詢編譯並快取至 XPathExpression 物件中,以便重複使用和提升效能。
編譯之後, XPathExpression 物件可能會作為下列 XPathNavigator 類別方法的輸入,視從 XPath 查詢傳回的類型而定。
下表描述了每個 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 物件之後,您可以使用 ReturnType 對象的 屬性 XPathExpression 來判斷 XPath 查詢傳回的內容。
屬性 ReturnType 會傳回代表 W3C XPath 傳回型別的下列 XPathResultType 其中一個列舉值。
下列範例使用XPathExpression 物件,從books.xml 檔案返還一個數字和一個節點集。
ReturnType每個XPathExpression物件的屬性以及Evaluate和Select方法的結果都會被寫入主控台。
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 的節點集流覽。