public abstract class XPathExpression
|
Expression | Read-only Gets a string representation of the XPathExpression. When overridden in a derived class, gets a string representation of the XPathExpression. |
ReturnType | Read-only Gets the result type of the XPathExpression as defined by the W3C XPath specification. |
AddSort | Overloaded:AddSort(object expr, IComparer comparer) Sorts the nodes selected by the XPathExpression, according to the IComparer interface. |
AddSort | Overloaded:AddSort(object expr, XmlSortOrder order, XmlCaseOrder caseOrder, string lang, XmlDataType dataType) Sorts the nodes selected by the XPathExpression according to the supplied parameters. |
Clone | Clones the XPathExpression. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
SetContext | Specifies the XmlNamespaceManager to use for resolving namespaces. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public abstract string Expression {get;}
|
public abstract XPathResultType ReturnType {get;}
|
XPathDocument doc = new XPathDocument("books.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. Evaluate(expr1, nav); Evaluate(expr2, nav); } public static void Evaluate(XPathExpression expr, XPathNavigator nav){ switch(expr.ReturnType) { case XPathResultType.Number: Console.WriteLine(nav.Evaluate(expr)); break; case XPathResultType.NodeSet: XPathNodeIterator i = nav.Select(expr); while (i.MoveNext()){ Console.WriteLine(i.Current.ToString()); } break; case XPathResultType.Boolean: if ((bool)nav.Evaluate(expr)) Console.WriteLine("True!"); break; case XPathResultType.String: Console.WriteLine(nav.Evaluate(expr)); break; } }
expr
comparer
Exception Type | Condition |
---|---|
XPathException | The XPathExpression or sort key includes a prefix and either an XmlNamepsaceManager is not provided, or the prefix cannot be found in the supplied XmlNamepsaceManager. |
In the following example, the books are sorted by ISBN number, where isbn is an object that implements the IComparer interface.
XPathExpression expr = nav.Compile("bookstore/book"); ISBN isbn = new ISBN(); expr.AddSort("@ISBN", (IComparer)isbn);
The order in which the sorts are added provides the sort key order.
If the XPathExpression or the sort key requires namespace resolution, you must use the XPathExpression.SetContext method to provide an XmlNamespaceManager for namespace resolution.
public abstract void AddSort( |
expr
order
caseOrder
lang
dataType
Exception Type | Condition |
---|---|
XPathException | The XPathExpression or sort key includes a prefix and either an XmlNamepsaceManager is not provided, or the prefix cannot be found in the supplied XmlNamepsaceManager. |
If you had the following XML
<bookstore> <book> <price>19.95</price> </book> ///more books </bookstore>
The following C# code processes the nodes in reverse document order and sorts the book prices numerically.
XPathExpression expr = nav.Compile("bookstore/book/price"); expr.AddSort("text()", XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Number);
If the XPathExpression or the sort key requires namespace resolution, you must use the XPathExpression.SetContext method to provide an XmlNamespaceManager for namespace resolution.
For example, if you had the following XML:
<bookstore xmlns="http://www.lucernepublishing.com"> <book> <title>Pride And Prejudice</title> </book> </bookstore>
The following C# code selects all book nodes:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable); nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com"); XPathExpression expr; expr = nav.Compile("//ab:book"); expr.SetContext(nsmgr); XPathNodeIterator iterator = nav.Select(expr);
using System; using System.IO; using System.Xml; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("booksort.xml"); XPathNavigator nav = doc.CreateNavigator(); //Select all books by Jane Austen. XPathExpression expr; expr = nav.Compile("descendant::book[author/last-name='Austen']"); //Sort the selected books by title. expr.AddSort("title", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text); //Display the selection. XPathNodeIterator iterator = nav.Select(expr); while (iterator.MoveNext()){ XPathNavigator nav2 = iterator.Current.Clone(); nav2.MoveToFirstChild(); Console.WriteLine("Book title: {0}", nav2.Value); } } }
The sample uses the following input file:
booksort.xml
<?xml version="1.0"?> <!-- a fragment of a book store inventory database --> <bookstore xmlns:bk="urn:samples"> <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8"> <title>Pride And Prejudice</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>24.95</price> </book> <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1"> <title>The Handmaid's Tale</title> <author> <first-name>Margaret</first-name> <last-name>Atwood</last-name> </author> <price>29.95</price> </book> <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6"> <title>Emma</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>19.95</price> </book> <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3"> <title>Sense and Sensibility</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>19.95</price> </book> </bookstore>
public abstract XPathExpression Clone(); |
~XPathExpression(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public abstract void SetContext( |
nsManager
Exception Type | Condition |
---|---|
XPathException | The nsManager is not derived from XmlNamespaceManager. |
<bookstore xmlns="http://www.lucernepublishing.com"> <book> <title>Pride And Prejudice</title> </book> </bookstore>The following C# code selects all book nodes:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable); nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com"); XPathExpression expr; expr = nav.Compile("//ab:book"); expr.SetContext(nsmgr); XPathNodeIterator iterator = nav.Select(expr);
using System; using System.IO; using System.Xml; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("booksort.xml"); XPathNavigator nav = doc.CreateNavigator(); //Select all ISBN attributes. XPathExpression expr; expr = nav.Compile("/bookstore/book/@bk:ISBN"); XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable); nsmgr.AddNamespace("bk", "urn:samples"); expr.SetContext(nsmgr); //Display the selection. XPathNodeIterator iterator = nav.Select(expr); while (iterator.MoveNext()){ Console.WriteLine(iterator.Current.ToString()); } } }
The sample uses the following input file:
booksort.xml
<?xml version="1.0"?> <!-- a fragment of a book store inventory database --> <bookstore xmlns:bk="urn:samples"> <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8"> <title>Pride And Prejudice</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>24.95</price> </book> <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1"> <title>The Handmaid's Tale</title> <author> <first-name>Margaret</first-name> <last-name>Atwood</last-name> </author> <price>29.95</price> </book> <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6"> <title>Emma</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>19.95</price> </book> <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3"> <title>Sense and Sensibility</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>19.95</price> </book> </bookstore>
public virtual string ToString(); |