Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

XPathNavigator.Evaluate Method (String)

.NET Framework 1.1

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( _
   ByVal xpath As String _
) As Object
[C#]
public virtual object Evaluate(
 string xpath
);
[C++]
public: virtual Object* Evaluate(
 String* xpath
);
[JScript]
public function Evaluate(
   xpath : String
) : Object;

Parameters

xpath
A string representing an XPath expression that can be evaluated.

Return Value

The typed result of the expression (number, Boolean, string, or node set). This maps to Double, Boolean, String, or XPathNodeIterator objects respectively.

Exceptions

Exception Type Condition
ArgumentException The XPath expression contains an error.

Remarks

The following C# code converts the Price/text() node to a number, multiplies it by 10, and returns the resulting value.

nav.Evaluate("Price/text()*10");
Note   The XPath position() and last() functions, unless used as a predicate in a location step, require a reference to a node set in order to be evaluated. In this case, you must use the overload which takes an XPathNodeIterator as an argument; otherwise, position() and last() return 0.

This method has no effect on the state of the XPathNavigator.

Example

[Visual Basic, C#, C++] The following example evaluates two XPath expressions.

[Visual Basic] 
Dim doc as XPathDocument = new XPathDocument("books.xml")
Dim nav as XPathNavigator = doc.CreateNavigator()

' Evaluate an expression that returns a number.
Dim expr1 as string = "sum(descendant::book/price)"
Console.WriteLine("Total price for all books: {0}", nav.Evaluate(expr1))

' Evaluate an expression that returns a node set.
Console.WriteLine("List each book price.")
Dim expr2 as string = "bookstore/book/price"
Dim ni as XPathNodeIterator = CType (nav.Evaluate(expr2), XPathNodeIterator)
while (ni.MoveNext())
  Console.WriteLine(ni.Current.ToString())
end while

[C#] 
XPathDocument doc = new XPathDocument("books.xml");
XPathNavigator nav = doc.CreateNavigator();

// Evaluate an expression that returns a number.
string expr1 = "sum(descendant::book/price)";
Console.WriteLine("Total price for all books: {0}", nav.Evaluate(expr1));

// Evaluate an expression that returns a node set.
Console.WriteLine("List each book price.");
string expr2 = "bookstore/book/price"; 
XPathNodeIterator ni = (XPathNodeIterator) nav.Evaluate(expr2);
while (ni.MoveNext()){
   Console.WriteLine(ni.Current.ToString());
}

[C++] 
XPathDocument* doc = new XPathDocument(S"books.xml");
XPathNavigator * nav = doc -> CreateNavigator();

// Evaluate an expression that returns a number.
String* expr1 = S"sum(descendant::book/price)";
Console::WriteLine(S"Total price for all books: {0}", nav -> Evaluate(expr1));

// Evaluate an expression that returns a node set.
Console::WriteLine(S"List each book price.");
String* expr2 = S"bookstore/book/price"; 
XPathNodeIterator * ni = dynamic_cast<XPathNodeIterator*>(nav -> Evaluate(expr2));
while (ni -> MoveNext()) 
{
   Console::WriteLine(ni -> Current);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XPathNavigator Class | XPathNavigator Members | System.Xml.XPath Namespace | XPathNavigator.Evaluate Overload List | Compile | XPathExpression | ReturnType

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.