XPathNavigator.Evaluate Method
Evaluates the given expression and returns the typed result.
Overload List
Evaluates the string representing an XPath expression and returns the typed result (number, Boolean, string, or node set). Use the XPathNodeIterator to iterate over a set of nodes.
[Visual Basic] Overloads Public Overridable Function Evaluate(String) As Object
[C#] public virtual object Evaluate(string);
[C++] public: virtual Object* Evaluate(String*);
[JScript] public function Evaluate(String) : Object;
Evaluates the XPathExpression and returns the typed result (number, Boolean, string, or node set). Use the XPathNodeIterator to iterate over a set of nodes.
[Visual Basic] Overloads Public Overridable Function Evaluate(XPathExpression) As Object
[C#] public virtual object Evaluate(XPathExpression);
[C++] public: virtual Object* Evaluate(XPathExpression*);
[JScript] public function Evaluate(XPathExpression) : Object;
Evaluates the XPathExpression using the supplied context and returns the typed result (number, Boolean, string, or node set).
[Visual Basic] Overloads Public Overridable Function Evaluate(XPathExpression, XPathNodeIterator) As Object
[C#] public virtual object Evaluate(XPathExpression, XPathNodeIterator);
[C++] public: virtual Object* Evaluate(XPathExpression*, XPathNodeIterator*);
[JScript] public function Evaluate(XPathExpression, XPathNodeIterator) : Object;
Example
[Visual Basic, C#, C++] The following example calculates the total price of a customer order.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of Evaluate. For other examples that might be available, see the individual overload topics.
[Visual Basic] Imports System Imports System.IO Imports System.Xml Imports System.Xml.XPath public class Sample public shared sub Main() Dim doc as XmlDocument = new XmlDocument() doc.Load("order.xml") Dim nav as XPathNavigator = doc.CreateNavigator() ' Calculate the total of the order. Dim expr as XPathExpression = nav.Compile("sum(//price/text())") Dim total as double = CType(nav.Evaluate(expr), double) ' If the total is more than 30 dollars, give the ' user a 5 dollar discount. if (total > 30.00) Dim disc as double = 5.00 total = total - disc Console.WriteLine("Total price: ${0}", total) else Console.WriteLine("Total price: ${0}", total) end if end sub end class [C#] using System; using System.IO; using System.Xml; using System.Xml.XPath; public class Sample { public static void Main() { XmlDocument doc = new XmlDocument(); doc.Load("order.xml"); XPathNavigator nav = doc.CreateNavigator(); // Calculate the total of the order. XPathExpression expr = nav.Compile("sum(//price/text())"); double total = (double) nav.Evaluate(expr); // If the total is more than 30 dollars, give the // user a 5 dollar discount. if (total > 30.00){ double disc = 5.00; total = total - disc; Console.WriteLine("Total price: ${0}", total); } else Console.WriteLine("Total price: ${0}", total); } } [C++] #using <mscorlib.dll> #using <System.Xml.dll> using namespace System; using namespace System::IO; using namespace System::Xml; using namespace System::Xml::XPath; int main() { XmlDocument* doc = new XmlDocument(); doc -> Load(S"order.xml"); XPathNavigator * nav = doc -> CreateNavigator(); // Calculate the total of the order. XPathExpression * expr = nav -> Compile(S"sum(//price/text())"); double total = *__try_cast<__box double*>(nav->Evaluate(expr)); // If the total is more than 30 dollars, give the // user a 5 dollar discount. if (total > 30.00) { double disc = 5.00; total = total - disc; Console::WriteLine(S"Total price: $ {0}", __box(total)); } else Console::WriteLine(S"Total price: $ {0}", __box(total)); }
The example uses the file, order.xml, as input.
<!--Represents a customer order-->
<order>
<book ISBN='10-861003-324'>
<title>The Handmaid's Tale</title>
<price>19.95</price>
</book>
<cd ISBN='2-3631-4'>
<title>Americana</title>
<price>16.95</price>
</cd>
</order>
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
See Also
XPathNavigator Class | XPathNavigator Members | System.Xml.XPath Namespace