XPathNavigator.Evaluate 方法

定義

評估指定的 XPath 運算式,並傳回具型別的結果。

多載

Evaluate(String)

評估指定的 XPath 運算式,並傳回具型別的結果。

Evaluate(XPathExpression)

評估 XPathExpression,並傳回具型別的結果。

Evaluate(String, IXmlNamespaceResolver)

評估指定的 XPath 運算式,並傳回具型別的結果,透過的方式是利用指定的 IXmlNamespaceResolver 物件來解析 XPath 運算式中的命名空間前置詞。

Evaluate(XPathExpression, XPathNodeIterator)

使用提供的內容來評估 XPathExpression,並傳回具型別的結果。

Evaluate(String)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

評估指定的 XPath 運算式,並傳回具型別的結果。

public:
 virtual System::Object ^ Evaluate(System::String ^ xpath);
public virtual object Evaluate (string xpath);
abstract member Evaluate : string -> obj
override this.Evaluate : string -> obj
Public Overridable Function Evaluate (xpath As String) As Object

參數

xpath
String

字串,表示能接受評估的 XPath 運算式。

傳回

運算式的結果 (布林值、數字、字串或節點集)。 這會分別對應至 BooleanDoubleStringXPathNodeIterator 等物件。

例外狀況

XPath 運算式的傳回型別為節點集。

XPath 運算式無效。

範例

下列範例會評估 XPath 運算式並傳 Double 回 。

XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();

Double total = (double)navigator->Evaluate("sum(descendant::book/price)");
Console::WriteLine("Total price for all books: {0}", total.ToString());
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

Double total = (double)navigator.Evaluate("sum(descendant::book/price)");
Console.WriteLine("Total price for all books: {0}", total.ToString());
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim total As Double = CType(navigator.Evaluate("sum(descendant::book/price)"), Double)
Console.WriteLine("Total price for all books: {0}", total.ToString())

範例將 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>  

備註

下列 C# 程式碼會將 Price/text() 節點轉換成數位,並將它乘以 10,並傳回產生的值。

nav.Evaluate("Price/text()*10");  

注意

除非做為位置步驟中的述詞使用 XPath position () 和 last () 函式,否則需要節點集的參考才能進行評估。 在此情況下,您必須使用接受 XPathNodeIterator 做為引數的多載;否則,position () 和 last () 會傳回 0。

這個方法不會影響 的狀態 XPathNavigator

另請參閱

適用於

Evaluate(XPathExpression)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

評估 XPathExpression,並傳回具型別的結果。

public:
 virtual System::Object ^ Evaluate(System::Xml::XPath::XPathExpression ^ expr);
public virtual object Evaluate (System.Xml.XPath.XPathExpression expr);
abstract member Evaluate : System.Xml.XPath.XPathExpression -> obj
override this.Evaluate : System.Xml.XPath.XPathExpression -> obj
Public Overridable Function Evaluate (expr As XPathExpression) As Object

參數

expr
XPathExpression

可以接受評估的 XPathExpression

傳回

運算式的結果 (布林值、數字、字串或節點集)。 這會分別對應至 BooleanDoubleStringXPathNodeIterator 等物件。

例外狀況

XPath 運算式的傳回型別為節點集。

XPath 運算式無效。

範例

下列範例會 XPathExpression 評估 ,並傳 Double 回 。

XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XPathExpression^ query = navigator->Compile("sum(descendant::book/price)");

Double total = (double)navigator->Evaluate(query);
Console::WriteLine("Total price for all books: {0}", total.ToString());
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathExpression query = navigator.Compile("sum(descendant::book/price)");

Double total = (double)navigator.Evaluate(query);
Console.WriteLine("Total price for all books: {0}", total.ToString());
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim query As XPathExpression = navigator.Compile("sum(descendant::book/price)")

Dim total As Double = CType(navigator.Evaluate(query), Double)
Console.WriteLine("Total price for all books: {0}", total.ToString())

範例將 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>  

備註

下列 C# 程式碼會在將節點轉換成 Price/text() 數位並將值乘以 10 之後傳回數位。

XPathExpression expr = nav.Compile("Price/text()*10");  
nav.Evaluate(expr);  

注意

除非做為位置步驟中的述詞使用 XPath position () 和 last () 函式,否則需要節點集的參考才能進行評估。 在此情況下,您必須使用接受 XPathNodeIterator 做為引數的多載;否則,position () 和 last () 會傳回 0。

這個方法不會影響 的狀態 XPathNavigator

另請參閱

適用於

Evaluate(String, IXmlNamespaceResolver)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

評估指定的 XPath 運算式,並傳回具型別的結果,透過的方式是利用指定的 IXmlNamespaceResolver 物件來解析 XPath 運算式中的命名空間前置詞。

public:
 virtual System::Object ^ Evaluate(System::String ^ xpath, System::Xml::IXmlNamespaceResolver ^ resolver);
public virtual object Evaluate (string xpath, System.Xml.IXmlNamespaceResolver? resolver);
public virtual object Evaluate (string xpath, System.Xml.IXmlNamespaceResolver resolver);
abstract member Evaluate : string * System.Xml.IXmlNamespaceResolver -> obj
override this.Evaluate : string * System.Xml.IXmlNamespaceResolver -> obj
Public Overridable Function Evaluate (xpath As String, resolver As IXmlNamespaceResolver) As Object

參數

xpath
String

字串,表示能接受評估的 XPath 運算式。

resolver
IXmlNamespaceResolver

IXmlNamespaceResolver 物件,用來解析 XPath 運算式中的命名空間前置詞。

傳回

運算式的結果 (布林值、數字、字串或節點集)。 這會分別對應至 BooleanDoubleStringXPathNodeIterator 等物件。

例外狀況

XPath 運算式的傳回型別為節點集。

XPath 運算式無效。

範例

下列範例會評估 XPath 運算式,並使用 DoubleXmlNamespaceManager 指定的 物件來解析 XPath 運算式中的命名空間前置詞。

XPathDocument^ document = gcnew XPathDocument("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XmlNamespaceManager^ manager = gcnew XmlNamespaceManager(navigator->NameTable);
manager->AddNamespace("bk", "http://www.contoso.com/books");

Double total = (double)navigator->Evaluate("sum(descendant::bk:book/bk:price)", manager);
Console::WriteLine("Total price for all books: {0}", total.ToString());
XPathDocument document = new XPathDocument("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");

Double total = (double)navigator.Evaluate("sum(descendant::bk:book/bk:price)", manager);
Console.WriteLine("Total price for all books: {0}", total.ToString());
Dim document As XPathDocument = New XPathDocument("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable)
manager.AddNamespace("bk", "http://www.contoso.com/books")

Dim total As Double = CType(navigator.Evaluate("sum(descendant::bk:book/bk:price)", manager), Double)
Console.WriteLine("Total price for all books: {0}", total.ToString())

範例將 contosoBooks.xml 檔案做為輸入。

<?xml version="1.0" encoding="utf-8" ?>  
<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>  

備註

下列 C# 程式碼會在將節點轉換成 Price/text() 數位並將值乘以 10 之後傳回數位。

XPathExpression expr = nav.Compile("Price/text()*10");  
nav.Evaluate(expr);  

注意

除非做為位置步驟中的述詞使用 XPath position () 和 last () 函式,否則需要節點集的參考才能進行評估。 在此情況下,您必須使用接受 XPathNodeIterator 做為引數的多載;否則,position () 和 last () 會傳回 0。

這個方法不會影響 的狀態 XPathNavigator

適用於

Evaluate(XPathExpression, XPathNodeIterator)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

使用提供的內容來評估 XPathExpression,並傳回具型別的結果。

public:
 virtual System::Object ^ Evaluate(System::Xml::XPath::XPathExpression ^ expr, System::Xml::XPath::XPathNodeIterator ^ context);
public virtual object Evaluate (System.Xml.XPath.XPathExpression expr, System.Xml.XPath.XPathNodeIterator? context);
public virtual object Evaluate (System.Xml.XPath.XPathExpression expr, System.Xml.XPath.XPathNodeIterator context);
abstract member Evaluate : System.Xml.XPath.XPathExpression * System.Xml.XPath.XPathNodeIterator -> obj
override this.Evaluate : System.Xml.XPath.XPathExpression * System.Xml.XPath.XPathNodeIterator -> obj
Public Overridable Function Evaluate (expr As XPathExpression, context As XPathNodeIterator) As Object

參數

expr
XPathExpression

可以接受評估的 XPathExpression

context
XPathNodeIterator

XPathNodeIterator,指向要執行評估的選定節點集。

傳回

運算式的結果 (布林值、數字、字串或節點集)。 這會分別對應至 BooleanDoubleStringXPathNodeIterator 等物件。

例外狀況

XPath 運算式的傳回型別為節點集。

XPath 運算式無效。

範例

下列範例會 XPathExpression 評估 ,並使用 的 XPathNodeIterator 節點做為內容節點傳回 DoubleCurrent

XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XPathNodeIterator^ nodes = navigator->Select("//book");
XPathExpression^ query = nodes->Current->Compile("sum(descendant::price)");

Double total = (double)navigator->Evaluate(query, nodes);
Console::WriteLine("Total price for all books: {0}", total.ToString());
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("//book");
XPathExpression query = nodes.Current.Compile("sum(descendant::price)");

Double total = (double)navigator.Evaluate(query, nodes);
Console.WriteLine("Total price for all books: {0}", total.ToString());
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim nodes As XPathNodeIterator = navigator.Select("//book")
Dim query As XPathExpression = nodes.Current.Compile("sum(descendant::price)")

Dim total As Double = CType(navigator.Evaluate(query, nodes), Double)
Console.WriteLine("Total price for all books: {0}", total.ToString())

範例將 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>  

備註

運算式會使用 CurrentXPathNodeIterator 節點做為內容節點進行評估。 如果 為 contextnull ,則會使用目前定位 所在的節點 XPathNavigator 做為內容節點。

position () 和 last () 函式,除非當做位置步驟中的述詞使用,否則在下列情況下一律會傳回 0:

因為 position () 和 last () 函式在目前節點上運作,所以您不應該使用 Current 屬性來離開選取的節點集。 這可能會使 的狀態 XPathNavigator 失效。

這個方法不會影響 的狀態 XPathNavigator

適用於