XPathExpression Class
Provides a typed class that represents a compiled XPath expression.
Assembly: System.Xml (in System.Xml.dll)
| Name | Description | |
|---|---|---|
![]() | Expression | When overridden in a derived class, gets a string representation of the XPathExpression. |
![]() | ReturnType | When overridden in a derived class, gets the result type of the XPath expression. |
| Name | Description | |
|---|---|---|
![]() | AddSort(Object^, IComparer^) | When overridden in a derived class, sorts the nodes selected by the XPath expression according to the specified IComparer object. |
![]() | AddSort(Object^, XmlSortOrder, XmlCaseOrder, String^, XmlDataType) | When overridden in a derived class, sorts the nodes selected by the XPath expression according to the supplied parameters. |
![]() | Clone() | When overridden in a derived class, returns a clone of this XPathExpression. |
![]() ![]() | Compile(String^) | Compiles the XPath expression specified and returns an XPathExpression object representing the XPath expression. |
![]() ![]() | Compile(String^, IXmlNamespaceResolver^) | Compiles the specified XPath expression, with the IXmlNamespaceResolver object specified for namespace resolution, and returns an XPathExpression object that represents the XPath expression. |
![]() | Equals(Object^) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetType() | |
![]() | MemberwiseClone() | |
![]() | SetContext(IXmlNamespaceResolver^) | When overridden in a derived class, specifies the IXmlNamespaceResolver object to use for namespace resolution. |
![]() | SetContext(XmlNamespaceManager^) | When overridden in a derived class, specifies the XmlNamespaceManager object to use for namespace resolution. |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
This class is returned as the result of the Compile method of the XPathNavigator class. It is a parameter to the Evaluate, Matches, Select, and SelectSingleNode methods of the XPathNavigator class.
Notes to Inheritors:
When you inherit from the XPathExpression class, you must override the following members:
The following example shows how to use the XPath return type to determine how to process the XPath expression.
public ref class Sample { public: static void Evaluate( XPathExpression^ expr, XPathNavigator^ nav ) { XPathNodeIterator^ i = nav->Select(expr); switch ( expr->ReturnType ) { case XPathResultType::Number: Console::WriteLine( nav->Evaluate( expr ) ); break; case XPathResultType::NodeSet: while ( i->MoveNext() ) Console::WriteLine( i->Current ); break; case XPathResultType::Boolean: if ( *safe_cast<bool^>(nav->Evaluate( expr )) ) Console::WriteLine( "True!" ); break; case XPathResultType::String: Console::WriteLine( nav->Evaluate( expr ) ); break; } } }; int main() { XPathDocument^ doc = gcnew XPathDocument( "contosoBooks.xml" ); XPathNavigator^ nav = doc->CreateNavigator(); XPathExpression^ expr1 = nav->Compile( ".//price/text()*10" ); // Returns a number. XPathExpression^ expr2 = nav->Compile( "bookstore/book/price" ); // Returns a nodeset. Sample^ MySample = gcnew Sample; MySample->Evaluate( expr1, nav ); MySample->Evaluate( expr2, nav ); }
The example takes the contosoBooks.xml file as input.
<?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>
Available since 1.1
Silverlight
Available since 4.0
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.



