This documentation is archived and is not being maintained.

XPathNavigator.SelectAncestors Method

Selects all the ancestor element nodes of the current node matching the selection criteria.

Overload List

Selects all the ancestor element nodes of the current node with the matching XPathNodeType.

[Visual Basic] Overloads Public Overridable Function SelectAncestors(XPathNodeType, Boolean) As XPathNodeIterator
[C#] public virtual XPathNodeIterator SelectAncestors(XPathNodeType, bool);
[C++] public: virtual XPathNodeIterator* SelectAncestors(XPathNodeType, bool);
[JScript] public function SelectAncestors(XPathNodeType, Boolean) : XPathNodeIterator;

Selects all the ancestor element nodes of the current node with the supplied local name and namespace URI.

[Visual Basic] Overloads Public Overridable Function SelectAncestors(String, String, Boolean) As XPathNodeIterator
[C#] public virtual XPathNodeIterator SelectAncestors(string, string, bool);
[C++] public: virtual XPathNodeIterator* SelectAncestors(String*, String*, bool);
[JScript] public function SelectAncestors(String, String, Boolean) : XPathNodeIterator;

Example

[Visual Basic, C#, C++] The following example selects all orders containing the specified CD.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of SelectAncestors. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Xml.XPath

public class Sample

  public shared sub Main()

    Dim doc as XPathDocument = new XPathDocument("order.xml")
    Dim nav as XPathNavigator = doc.CreateNavigator()

    ' Select all cd nodes with the specified ISBN value.
    Dim ni as XPathNodeIterator = nav.Select("//cd[@ISBN='2-3631-4']")

    ' Select all orders containing the selected cd node.
    while (ni.MoveNext())
       Dim nav2 as XPathNavigator = ni.Current
       Dim ni2 as XPathNodeIterator = nav2.SelectAncestors("order", "", false)
       while (ni2.MoveNext())
         Console.WriteLine(ni2.Current.ToString())   
       end while
   end while

  end sub
end class

[C#] 
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());   
   }

  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml::XPath;

int main()
{
   XPathDocument* doc = new XPathDocument(S"order.xml");
   XPathNavigator * nav = doc -> CreateNavigator();

   // Select all cd nodes with the specified ISBN value.
   XPathNodeIterator * ni = nav -> Select(S"//cd[@ISBN='2-3631-4']");

   // Select all orders containing the selected cd node.
   while (ni -> MoveNext()) 
   {
      XPathNavigator * nav2 = ni -> Current;
      XPathNodeIterator * ni2 = nav2 -> SelectAncestors(S"order", S"", false);
      while (ni2 -> MoveNext())
         Console::WriteLine(ni2 -> Current);   
   }
}

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 Language Filter in the upper-left corner of the page.

See Also

XPathNavigator Class | XPathNavigator Members | System.Xml.XPath Namespace

Show: