共用方式為


編譯 XPath 運算式

XPathExpression 物件表示從 Compile 類別的靜態 XPathExpression 方法或 Compile 類別的 XPathNavigator 方法傳回的編譯 XPath 查詢。

XPathExpression 類別

如果會多次使用同一 XPath 查詢,則 XPathExpression 物件所表示的編譯 XPath 查詢會很有用。

例如,多次呼叫 Select 方法,而不是每次都使用表示 XPath 查詢的字串時,請使用 Compile 類別的 XPathExpression 方法或 Compile 類別的 XPathNavigator 方法,來編譯並快取 XPathExpression 物件中的 XPath 查詢,以重複使用並提升效能。

編譯後,根據從 XPath 查詢傳回的型別,可能會使用 XPathExpression 物件做為下列 XPathNavigator 類別方法的輸入。

下表說明每個 W3C XPath 傳回型別、其 Microsoft .NET Framework 對等型別,以及與 XPathExpression 物件搭配使用的方法 (根據其傳回型別)。

W3C XPath 傳回型別 .NET Framework 對等型別 描述 方法
Node set XPathNodeIterator 依文件順序建立之不重複節點的未排序集合。 SelectEvaluate
Boolean Boolean truefalse 值。 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 巡覽節點集

另請參閱