XPathNavigator.Evaluate 方法

定义

计算指定的 XPath 表达式并返回类型化结果。

重载

Evaluate(String)

计算指定的 XPath 表达式并返回类型化结果。

Evaluate(XPathExpression)

计算 XPathExpression 并返回类型化结果。

Evaluate(String, IXmlNamespaceResolver)

计算指定的 XPath 表达式并返回类型化结果,以使用指定的 IXmlNamespaceResolver 对象解析 XPath 表达式中的命名空间前缀。

Evaluate(XPathExpression, XPathNodeIterator)

使用提供的上下文计算 XPathExpression,并返回类型化结果。

Evaluate(String)

计算指定的 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 表达式的字符串。

返回

Object

表达式结果(布尔值、数字、字符串或节点集)。 这分别映射到 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)

计算 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

返回

Object

表达式结果(布尔值、数字、字符串或节点集)。 这分别映射到 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)

计算指定的 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

用来解析 XPath 表达式中的命名空间前缀的 IXmlNamespaceResolver 对象。

返回

Object

表达式结果(布尔值、数字、字符串或节点集)。 这分别映射到 BooleanDoubleStringXPathNodeIterator 对象。

例外

XPath 表达式的返回类型是节点集。

XPath 表达式无效。

示例

以下示例计算 XPath 表达式,并返回一个 Double 使用 XmlNamespaceManager 指定的对象解析 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)

使用提供的上下文计算 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,指向要在其上执行计算的选定节点集。

返回

Object

表达式结果(布尔值、数字、字符串或节点集)。 这分别映射到 BooleanDoubleStringXPathNodeIterator 对象。

例外

XPath 表达式的返回类型是节点集。

XPath 表达式无效。

示例

以下示例计算一个并使用上下文节点作为上下文节点的节点返回一个XPathExpression Current DoubleXPathNodeIterator

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>  

注解

表达式是使用 Current 上下文节点的 XPathNodeIterator 节点计算的。 null如果是context,则当前定位的XPathNavigator节点用作上下文节点。

position () 和 last () 函数(除非用作位置步骤中的谓词),否则在以下条件下始终返回 0:

由于位置 () 和最后一个 () 函数在当前节点上工作,因此不应使用 Current 属性远离所选节点集。 这可能会使状态 XPathNavigator失效。

此方法对状态 XPathNavigator无效。

适用于