public abstract class XPathNavigator : ICloneable
|
XPathNavigator provides read-only, random access to data. The current node refers to the node on which the navigator is positioned. The navigator is advanced using any of the move methods and properties reflect the value of the current node. If the navigator is created by XmlDocument, you can use the IHasXmlNode.GetNode method to return the XmlNode representing the current position of the navigator.
XPathNavigator supports XSLT and can be used as an input mechanism to the XslTransform.Transform method.
For more information on the XPath data model, see the W3C XML Path Language (XPath) recommendation located at http://www.w3.org/TR/xpath.
BaseURI | Read-only Gets the base URI for the current node. |
HasAttributes | Read-only Gets a value indicating whether the element node has any attributes. |
HasChildren | Read-only Gets a value indicating whether the current node has child nodes. |
IsEmptyElement | Read-only Gets a value indicating whether the current node is an empty element (for example, <MyElement/>). |
LocalName | Read-only Gets the name of the current node without the namespace prefix. |
Name | Read-only Gets the qualified name of the current node. |
NamespaceURI | Read-only Gets the namespace URI (as defined in the W3C Namespace Specification) of the current node. |
NameTable | Read-only Gets the XmlNameTable associated with this implementation. |
NodeType | Read-only Gets the type of the current node. |
Prefix | Read-only Gets the prefix associated with the current node. |
Value | Read-only Gets the text value of the current node. |
XmlLang | Read-only Gets the xml:lang scope for the current node. |
Clone | Creates a new XPathNavigator positioned at the same node as this XPathNavigator. |
ComparePosition | Compares the position of the current navigator with the position of the specified XPathNavigator. |
Compile | Compiles a string representing an XPath expression and returns an 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. |
Evaluate | Overloaded:Evaluate(string xpath) 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. |
Evaluate | Overloaded:Evaluate(XPathExpression expr) Evaluates the XPathExpression and returns the typed result (number, Boolean, string, or node set). Use the XPathNodeIterator to iterate over a set of nodes. |
Evaluate | Overloaded:Evaluate(XPathExpression expr, XPathNodeIterator context) Evaluates the XPathExpression using the supplied context and returns the typed result (number, Boolean, string, or node set). |
GetAttribute | Gets the value of the attribute with the specified XPathNavigator.LocalName and XPathNavigator.NamespaceURI. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetNamespace | Returns the value of the namespace node corresponding to the specified local name. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsDescendant | Determines whether the specified XPathNavigator is a descendant of the current XPathNavigator. |
IsSamePosition | Determines whether the current XPathNavigator is at the same position as the specified XPathNavigator. |
Matches | Overloaded:Matches(string xpath) Determines whether the current node matches the specified XSLT pattern. |
Matches | Overloaded:Matches(XPathExpression expr) Determines whether the current node matches the specified XPathExpression. |
MoveTo | Moves to the same position as the specified XPathNavigator. |
MoveToAttribute | Moves to the attribute with matching XPathNavigator.LocalName and XPathNavigator.NamespaceURI. |
MoveToFirst | Moves to the first sibling of the current node. |
MoveToFirstAttribute | Moves to the first attribute. |
MoveToFirstChild | Moves to the first child of the current node. |
MoveToFirstNamespace | Overloaded:MoveToFirstNamespace() Moves the XPathNavigator to first namespace node of the current element. |
MoveToFirstNamespace | Overloaded:MoveToFirstNamespace(XPathNamespaceScope namespaceScope) Moves the XPathNavigator to the first namespace node matching the XPathNamespaceScope specified. |
MoveToId | Moves to the node that has an attribute of type ID whose value matches the specified string. |
MoveToNamespace | Moves the XPathNavigator to the namespace node with the specified local name. |
MoveToNext | Moves to the next sibling of the current node. |
MoveToNextAttribute | Moves to the next attribute. |
MoveToNextNamespace | Overloaded:MoveToNextNamespace() Moves the XPathNavigator to the next namespace node. |
MoveToNextNamespace | Overloaded:MoveToNextNamespace(XPathNamespaceScope namespaceScope) Moves the XPathNavigator to the next namespace node matching the XPathNamespaceScope specified. |
MoveToParent | Moves to the parent of the current node. |
MoveToPrevious | Moves to the previous sibling of the current node. |
MoveToRoot | Moves to the root node to which the current node belongs. |
Select | Overloaded:Select(string xpath) Selects a node set using the specified XPath expression. |
Select | Overloaded:Select(XPathExpression expr) Selects a node set using the specified XPathExpression. |
SelectAncestors | Overloaded:SelectAncestors(XPathNodeType type, bool matchSelf) Selects all the ancestor element nodes of the current node with the matching XPathNodeType. |
SelectAncestors | Overloaded:SelectAncestors(string name, string namespaceURI, bool matchSelf) Selects all the ancestor element nodes of the current node with the supplied local name and namespace URI. |
SelectChildren | Overloaded:SelectChildren(XPathNodeType type) Selects all the child nodes of the current node with the matching XPathNodeType. |
SelectChildren | Overloaded:SelectChildren(string name, string namespaceURI) Selects all the child element nodes of the current node with the supplied local name and namespace URI. |
SelectDescendants | Overloaded:SelectDescendants(XPathNodeType type, bool matchSelf) Selects all the descendant nodes of the current node with the matching XPathNodeType. |
SelectDescendants | Overloaded:SelectDescendants(string name, string namespaceURI, bool matchSelf) Selects all the descendant element nodes of the current node with the supplied local name and namespace URI. |
ToString | Overridden: Gets the text value of the current node. This method is equivalent to returning the XPathNavigator.Value property. |
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. |
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:
protected XPathNavigator(); |
public abstract string BaseURI {get;}
|
For example, if the XML data is loaded from http://server/mydata.xml then BaseURI returns http://server/mydata.xml.
public abstract bool HasAttributes {get;}
|
public abstract bool HasChildren {get;}
|
XPathNodeType | Child Nodes |
---|---|
Root | Element, comment or processing instruction nodes. |
Element | Element, text, comment, whites pace or significant white space nodes. |
All other XpathNodeType nodes | These node types cannot have child nodes. |
public abstract bool IsEmptyElement {get;}
|
<item bar="123"/>
(IsEmptyElement is true).
<item
bar="123"></item>
(IsEmptyElement is false).
<item bar="123">test</item>
(IsEmptyElement is false).
public abstract string LocalName {get;}
|
public static void RecursiveWalk(XPathNavigator nav) { switch (nav.NodeType){ case XPathNodeType.Element: if (nav.Prefix==String.Empty) Console.WriteLine("<{0}>", nav.LocalName); else Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName); Console.WriteLine("\t"+ nav.NamespaceURI); break; case XPathNodeType.Text: Console.WriteLine("\t" + nav.Value); break; } if ( nav.MoveToFirstChild() ) { do{ RecursiveWalk(nav); } while ( nav.MoveToNext() ); nav.MoveToParent(); if (nav.NodeType == XPathNodeType.Element) Console.WriteLine("</{0}>", nav.Name); } }
public abstract string Name {get;}
|
public static void RecursiveWalk(XPathNavigator nav) { switch (nav.NodeType){ case XPathNodeType.Element: if (nav.Prefix==String.Empty) Console.WriteLine("<{0}>", nav.LocalName); else Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName); Console.WriteLine("\t"+ nav.NamespaceURI); break; case XPathNodeType.Text: Console.WriteLine("\t" + nav.Value); break; } if ( nav.MoveToFirstChild() ) { do{ RecursiveWalk(nav); } while ( nav.MoveToNext() ); nav.MoveToParent(); if (nav.NodeType == XPathNodeType.Element) Console.WriteLine("</{0}>", nav.Name); } }
public abstract string NamespaceURI {get;}
|
public static void RecursiveWalk(XPathNavigator nav) { switch (nav.NodeType){ case XPathNodeType.Element: if (nav.Prefix==String.Empty) Console.WriteLine("<{0}>", nav.LocalName); else Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName); Console.WriteLine("\t"+ nav.NamespaceURI); break; case XPathNodeType.Text: Console.WriteLine("\t" + nav.Value); break; } if ( nav.MoveToFirstChild() ) { do{ RecursiveWalk(nav); } while ( nav.MoveToNext() ); nav.MoveToParent(); if (nav.NodeType == XPathNodeType.Element) Console.WriteLine("</{0}>", nav.Name); } }
public abstract XmlNameTable NameTable {get;}
|
For more information on atomized strings, see XmlNameTable.
public abstract XPathNodeType NodeType {get;}
|
public static void RecursiveWalk(XPathNavigator nav) { switch (nav.NodeType){ case XPathNodeType.Element: if (nav.Prefix==String.Empty) Console.WriteLine("<{0}>", nav.LocalName); else Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName); Console.WriteLine("\t"+ nav.NamespaceURI); break; case XPathNodeType.Text: Console.WriteLine("\t" + nav.Value); break; } if ( nav.MoveToFirstChild() ) { do{ RecursiveWalk(nav); } while ( nav.MoveToNext() ); nav.MoveToParent(); if (nav.NodeType == XPathNodeType.Element) Console.WriteLine("</{0}>", nav.Name); } }
public abstract string Prefix {get;}
|
public static void RecursiveWalk(XPathNavigator nav) { switch (nav.NodeType){ case XPathNodeType.Element: if (nav.Prefix==String.Empty) Console.WriteLine("<{0}>", nav.LocalName); else Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName); Console.WriteLine("\t"+ nav.NamespaceURI); break; case XPathNodeType.Text: Console.WriteLine("\t" + nav.Value); break; } if ( nav.MoveToFirstChild() ) { do{ RecursiveWalk(nav); } while ( nav.MoveToNext() ); nav.MoveToParent(); if (nav.NodeType == XPathNodeType.Element) Console.WriteLine("</{0}>", nav.Name); } }
public abstract string Value {get;}
|
public static void RecursiveWalk(XPathNavigator nav) { switch (nav.NodeType){ case XPathNodeType.Element: if (nav.Prefix==String.Empty) Console.WriteLine("<{0}>", nav.LocalName); else Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName); Console.WriteLine("\t"+ nav.NamespaceURI); break; case XPathNodeType.Text: Console.WriteLine("\t" + nav.Value); break; } if ( nav.MoveToFirstChild() ) { do{ RecursiveWalk(nav); } while ( nav.MoveToNext() ); nav.MoveToParent(); if (nav.NodeType == XPathNodeType.Element) Console.WriteLine("</{0}>", nav.Name); } }
public abstract string XmlLang {get;}
|
<root xml:lang="en-us"> <name>Fred</name> </root>
public abstract XPathNavigator Clone(); |
The cloned XPathNavigator is not affected by subsequent changes to the original XPathNavigator.
using System; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("booksort.xml"); XPathNavigator nav = doc.CreateNavigator(); // Select all books authored by Austen. XPathNodeIterator ni = nav.Select("descendant::book[author/last-name='Austen']"); while (ni.MoveNext()){ // Clone the navigator returned by the Current property. // Use the cloned the navigator to get the title element. XPathNavigator clone = ni.Current.Clone(); clone.MoveToFirstChild(); Console.WriteLine("Book title: {0}", clone.Value); } } }
The example uses the booksort.xml file as input.
<?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 XmlNodeOrder ComparePosition( |
nav
XmlNodeOrder | Description |
---|---|
Before | The current node of this navigator is before the current node of the supplied navigator. |
After | The current node of this navigator is after the current node of the supplied navigator. |
Same | The two navigators are positioned on the same node. |
Unknown | The node positions cannot be determined in document order, relative to each other. This could occur if the two nodes reside in different trees. |
Exception Type | Condition |
---|---|
Exception | The nav parameter is null. |
This method has no effect on the state of the XPathNavigator.
public virtual XPathExpression Compile( |
xpath
Exception Type | Condition |
---|---|
ArgumentException | The xpath parameter contains an invalid XPath expression. |
Expressions that return a node set can be used in the XPathNavigator.Select and XPathNavigator.Evaluate methods. Expressions that return a boolean, number, or string can be used in the XPathNavigator.Evaluate method. The rules on valid expressions for the XPathNavigator.Matches method are specific to that method.
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 example uses the file, booksort.xml, as input.
<?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>
xpath
Exception Type | Condition |
---|---|
ArgumentException | The XPath expression contains an error. |
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.
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()); }
public virtual object Evaluate( |
expr
Exception Type | Condition |
---|---|
ArgumentException | The XPathExpression contains an error. |
XPathExpression expr = nav.Compile("Price/text()*10"); nav.Evaluate(expr);
This method has no effect on the state of the XPathNavigator.
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); } }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>
public virtual object Evaluate( |
expr
context
Exception Type | Condition |
---|---|
ArgumentException | The XPathExpression contains an error. |
The position() and last() functions, unless used as a predicate in a location step, always return 0 under the following conditions:
Because the position() and last() functions work on the current node, you should not use the XPathNodeIterator.Current property to move away from the selected node set. This could invalidate the state of the XPathNavigator.
This method has no effect on the state of the XPathNavigator.
~XPathNavigator(); |
localName
namespaceURI
public virtual int GetHashCode(); |
name
public Type GetType(); |
public virtual bool IsDescendant( |
nav
<item><name>widget</name></item>
If the current navigator is positioned on the item node and nav is positioned on the name node, IsDescendant returns true.
This method always returns false under the following conditions:
public abstract bool IsSamePosition( |
other
This method always returns false under the following conditions:
xpath
Exception Type | Condition |
---|---|
ArgumentException | The xpath cannot be evaluated. |
public virtual bool Matches( |
expr
Exception Type | Condition |
---|---|
ArgumentException | The expr cannot be evaluated. |
using System; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("books.xml"); XPathNavigator nav = doc.CreateNavigator(); // Select all book nodes. XPathNodeIterator ni = nav.SelectDescendants("book", "", false); // Select all book nodes that have the matching attribute value. XPathExpression expr = nav.Compile("book[@genre='novel']"); while (ni.MoveNext()) { XPathNavigator nav2 = ni.Current.Clone(); if (nav2.Matches(expr)){ nav2.MoveToFirstChild(); Console.WriteLine("Book title: {0}", nav2.Value); } } } }The example uses the file, books.xml, as input.
<bookstore> <book genre="autobiography" publicationdate="1981" 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" 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" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore>
protected object MemberwiseClone(); |
public abstract bool MoveTo( |
other
This method always returns false if other is an XPathNavigator that has a different implementation than the current navigator.
If other is an XPathNavigator that shares the same implementation but is pointing at a different document instance than the current navigator, the behavior of MoveTo varies depending on how the navigators were implemented. If the navigators were implemented over an XPathDocument, MoveTo returns true. If the navigators were implemented over either an XmlDocument or an XmlDataDocument, MoveTo returns false.
localName
namespaceURI
After a successful call to MoveToAttribute, the LocalName, NamespaceURI and XPathNavigator.Prefix properties reflect the values of the attribute. When the navigator is positioned on an attribute, the methods XPathNavigator.MoveToNext, XPathNavigator.MoveToPrevious, and XPathNavigator.MoveToFirst are not applicable. These methods always return false and do not change the position of the navigator. Rather, you can call XPathNavigator.MoveToNextAttribute to move to the next attribute node.
Once positioned on an attribute, you can call XPathNavigator.MoveToParent to move to the owner element.
public abstract bool MoveToFirst(); |
public abstract bool MoveToFirstAttribute(); |
After a successful call to MoveToFirstAttribute, the LocalName, NamespaceURI and XPathNavigator.Prefix properties reflect the values of the attribute. When the navigator is positioned on an attribute, the methods XPathNavigator.MoveToNext, XPathNavigator.MoveToPrevious, and XPathNavigator.MoveToFirst are not applicable. These methods always return false and do not change the position of the navigator. Rather, you can call XPathNavigator.MoveToNextAttribute to move to the next attribute node.
Once positioned on an attribute, you can call XPathNavigator.MoveToParent to move to the owner element.
public abstract bool MoveToFirstChild(); |
public bool MoveToFirstNamespace(); |
After the navigator has been moved to the namespace node the XPathNavigator.Name property reflects the name of the namespace.
When the navigator is positioned on a namespace node, the methods XPathNavigator.MoveToNext, XPathNavigator.MoveToPrevious, and XPathNavigator.MoveToFirst are not applicable. These methods always return false and do not change the position of the navigator. Rather, you can call XPathNavigator.MoveToNextNamespace to move to the next namespace node.
Once positioned on an namespace node, call XPathNavigator.MoveToParent to move back to the element node.
For a definition of namespace nodes, see section 5.4 of the W3C XML Path Language (XPath) recommendation located at http://www.w3.org/TR/xpath#data-model .
public abstract bool MoveToFirstNamespace( |
namespaceScope
xmlns:c='C'
,
xmlns:a='A2'
,
xmlns=''
,
xmlns:b='B'
, and
xmlns:xml='http://www.w3.org/XML/1998/namespace'
.
xmlns:c='C'
,
xmlns:a='A2'
,
xmlns=''
, and
xmlns:b='B'
.
xmlns:c='C'
node.<root xmlns='urn:default' xmlns:a='A1' xmlns:b='B'> <item xmlns='' xmlns:a='A2'> <bar xmlns:c='C' /> </item> </root>
If the navigator is not currently positioned on an element, this method returns false and the position of the navigator does not change. After the navigator has been moved to the namespace node the XPathNavigator.Name property reflects the name of the namespace.
When the navigator is positioned on a namespace node, the methods XPathNavigator.MoveToNext, XPathNavigator.MoveToPrevious, and XPathNavigator.MoveToFirst are not applicable. These methods always return false and do not change the position of the navigator. Rather, you can call XPathNavigator.MoveToNextNamespace to move to the next namespace node.
Once positioned on an namespace node, call XPathNavigator.MoveToParent to move back to the element node.
For a definition of namespace nodes, see section 5.4 of the W3C XML Path Language (XPath) recommendation located at http://www.w3.org/TR/xpath#data-model .
id
For example, suppose the following XML data,
<data><x a="a1"/></data>
, was loaded using a DTD that included the following:
<!ATTLIST x a ID
#REQUIRED>
.
If the navigator was positioned somewhere in the XML data, calling MoveToId("a1") positions the navigator on the element node x.
name
When the navigator is positioned on a namespace node, the methods XPathNavigator.MoveToNext, XPathNavigator.MoveToPrevious, and XPathNavigator.MoveToFirst are not applicable. These methods always return false and do not change the position of the navigator. Rather, you can call XPathNavigator.MoveToNextNamespace to move to the next namespace node.
Once positioned on a namespace node, you can call XPathNavigator.MoveToParent to move back to the element node.
For a definition of namespace nodes, see section 5.4 of the W3C XML Path Language (XPath) recommendation located at http://www.w3.org/TR/xpath#data-model.
public abstract bool MoveToNext(); |
public static void RecursiveWalk(XPathNavigator nav) { switch (nav.NodeType){ case XPathNodeType.Element: if (nav.Prefix==String.Empty) Console.WriteLine("<{0}>", nav.LocalName); else Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName); Console.WriteLine("\t"+ nav.NamespaceURI); break; case XPathNodeType.Text: Console.WriteLine("\t" + nav.Value); break; } if ( nav.MoveToFirstChild() ) { do{ RecursiveWalk(nav); } while ( nav.MoveToNext() ); nav.MoveToParent(); if (nav.NodeType == XPathNodeType.Element) Console.WriteLine("</{0}>", nav.Name); } }
public abstract bool MoveToNextAttribute(); |
When the navigator is positioned on an attribute, the methods XPathNavigator.MoveToNext, XPathNavigator.MoveToPrevious, and XPathNavigator.MoveToFirst are not applicable. These methods always return false and do not change the position of the navigator.
Once positioned on an attribute, you can call XPathNavigator.MoveToParent to move to the owner element.
using System; using System.IO; using System.Xml; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("books.xml"); XPathNavigator nav = doc.CreateNavigator(); // Select all book nodes and display all attributes on each book. XPathNodeIterator ni = nav.SelectDescendants("book", "", false); while (ni.MoveNext()){ XPathNavigator nav2 = ni.Current.Clone(); nav2.MoveToFirstAttribute(); Console.WriteLine("{0} = {1}", nav2.Name, nav2.Value); while (nav2.MoveToNextAttribute()){ Console.WriteLine("{0} = {1}", nav2.Name, nav2.Value); } Console.WriteLine(); } } }
public bool MoveToNextNamespace(); |
When the navigator is positioned on a namespace node, the methods XPathNavigator.MoveToNext, XPathNavigator.MoveToPrevious, and XPathNavigator.MoveToFirst are not applicable. These methods always return false and do not change the position of the navigator.
Once positioned on an namespace node, call XPathNavigator.MoveToParent to move back to the element node.
For a definition of namespace nodes, see section 5.4 of the W3C XML Path Language (XPath) recommendation located at http://www.w3.org/TR/xpath#data-model.
public abstract bool MoveToNextNamespace( |
namespaceScope
When the navigator is positioned on a namespace node, the methods XPathNavigator.MoveToNext, XPathNavigator.MoveToPrevious, and XPathNavigator.MoveToFirst are not applicable. These methods always return false and do not change the position of the navigator.
Once positioned on an namespace node, call XPathNavigator.MoveToParent to move back to the element node.
For a definition of namespace nodes, see section 5.4 of the W3C XML Path Language (XPath) recommendation located at http://www.w3.org/TR/xpath#data-model.
public abstract bool MoveToParent(); |
XPathNodeType | Parent Node |
---|---|
Root | Root nodes do not have parents. |
Element | Element or Root node. |
Attribute | Element node. |
Text | Element node. |
Namespace | Element node. |
ProcessingInstruction | Element or Root node. |
Comment | Element or Root node. |
Whitespace | Element or Root node. |
SignificantWhitespace | Element or Root node. |
public abstract bool MoveToPrevious(); |
public abstract void MoveToRoot(); |
public virtual XPathNodeIterator Select( |
xpath
Exception Type | Condition |
---|---|
ArgumentException | The xpath contains an error or its return type is not a node set. |
The following C# code walks the selected set of nodes.
XPathNodeIterator iterator = nav.Select("/bookstore/book"); while (iterator.MoveNext()){ Console.WriteLine(Iterator.Current.Name); }
If the XPath expression requires namespace resolution, use the overload which takes an XPathExpression as its argument.
Calling Select again returns a new XPathNodeIterator pointing to the selected set of nodes matching the new Select call. The two XPathNodeIterator objects are completely independent of each other.
This method has no effect on the state of the XPathNavigator.
using System; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("books.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator ni = nav.Select("/bookstore/book/title"); while (ni.MoveNext()) Console.WriteLine(ni.Current.Value); } }The example uses the file, books.xml, as input.
<bookstore> <book genre="autobiography" publicationdate="1981" 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" 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" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore>
public virtual XPathNodeIterator Select( |
expr
Exception Type | Condition |
---|---|
ArgumentException | The expr contains an error or its return type is not a node set. |
The following C# code walks the selected set of nodes.
XPathNodeIterator iterator = nav.Select(expr); while (iterator.MoveNext()){ Console.WriteLine(Iterator.Current.Name); }
If the XPathExpression requires namespace resolution, the prefix and namespace URI pair must be added to an XmlNamespaceManager and the XPathExpression.SetContext method must be called to specify the XmlNamespaceManager to use for namespace resolution.
For example, if the document contains the XML nodes
<bookstore xmlns:bk='urn:samples'> <book bk:ISBN='1-325-0980'> <title>Pride And Prejudice</title> </book> </bookstore>
The following C# code selects the bk:ISBN node.
XPathExpression expr = nav.Compile("book/@bk:ISBN"); XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable()); mngr.AddNamespace("bk","urn:samples"); expr.SetContext(mngr); nav.Select(expr)
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);
Calling Select again returns a new XPathNodeIterator pointing to the selected set of nodes matching the new Select call. The two XPathNodeIterator objects are completely independent of each other.
This method has no effect on the state of the XPathNavigator.
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 example uses the file, booksort.xml, as input.
<?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 XPathNodeIterator SelectAncestors( |
type
matchSelf
public virtual XPathNodeIterator SelectAncestors( |
name
namespaceURI
matchSelf
If name is an empty string, all ancestor nodes belonging to the specified namespace URI are selected. If namespaceURI is an empty string, all ancestor nodes with the specified name belonging to the null namespace are selected. If both name and namespaceURI are empty strings, all ancestor nodes belonging to the null namespace are selected.
using System; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("order.xml"); XPathNavigator nav = doc.CreateNavigator(); // Select all cd nodes with the specified ISBN value. XPathNodeIterator ni = nav.Select("//cd[@ISBN='2-3631-4']"); // Select all orders containing the selected cd node. while (ni.MoveNext()){ XPathNavigator nav2 = ni.Current; XPathNodeIterator ni2 = nav2.SelectAncestors("order", "", false); while (ni2.MoveNext()) Console.WriteLine(ni2.Current.ToString()); } } }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>
public virtual XPathNodeIterator SelectChildren( |
type
The selected nodes will never contain attribute or namespace nodes.
public virtual XPathNodeIterator SelectChildren( |
name
namespaceURI
If name is an empty string, all child nodes belonging to the specified namespace URI are selected. If namespaceURI is an empty string, all child nodes with the specified name belonging to the null namespace are selected. If both name and namespaceURI are empty strings, all child nodes belonging to the null namespace are selected.
The selected nodes will never contain attribute or namespace nodes.
public virtual XPathNodeIterator SelectDescendants( |
type
matchSelf
A descendant is a child or a child of a child and so on; thus, the selected nodes will never contain attribute or namespace nodes.
using System; using System.IO; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("books.xml"); XPathNavigator nav = doc.CreateNavigator(); // Move to the first book node. XPathNodeIterator ni = nav.SelectDescendants("book", "", false); ni.MoveNext(); XPathNavigator nav2 = ni.Current; // Display the content of each element node. XPathNodeIterator ni2 = nav2.SelectDescendants(XPathNodeType.Text, false); while (ni2.MoveNext()){ Console.WriteLine(ni2.Current.Value); } } }The example uses the file, books.xml, as input.
<bookstore> <book genre="autobiography" publicationdate="1981" 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" 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" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore>
public virtual XPathNodeIterator SelectDescendants( |
name
namespaceURI
matchSelf
If name is an empty string, all descendant nodes belonging to the specified namespace URI are selected. If namespaceURI is an empty string, all descendant nodes with the specified name belonging to the null namespace are selected. If both name and namespaceURI are empty strings, all descendant nodes belonging to the null namespace are selected.
A descendant is a child or a child of a child and so on; thus the selected nodes will never contain attribute or namespace nodes.
public override string ToString(); |
Node Type | Value |
---|---|
Attribute | The value of the attribute. |
Element | The XmlNode.InnerText of the element. |
Comment | The content of the comment. |
ProcessingInstruction | The entire content excluding the target. |
Text | The content of the text node. |
Whitespace | The white space between mark-up. |
SignificantWhitespace | The white space between markup in a mixed content model or white space within an xml:space= 'preserve' scope. |
Root | The XmlNode.InnerText of the root node. |