XPathNavigator.SelectAncestors Method

Definition

Selects all the ancestor nodes of the current node that match the selection criteria.

Overloads

SelectAncestors(XPathNodeType, Boolean)

Selects all the ancestor nodes of the current node that have a matching XPathNodeType.

SelectAncestors(String, String, Boolean)

Selects all the ancestor nodes of the current node that have the specified local name and namespace URI.

SelectAncestors(XPathNodeType, Boolean)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

Selects all the ancestor nodes of the current node that have a matching XPathNodeType.

C#
public virtual System.Xml.XPath.XPathNodeIterator SelectAncestors(System.Xml.XPath.XPathNodeType type, bool matchSelf);

Parameters

type
XPathNodeType

The XPathNodeType of the ancestor nodes.

matchSelf
Boolean

To include the context node in the selection, true; otherwise, false.

Returns

An XPathNodeIterator that contains the selected nodes. The returned nodes are in reverse document order.

Examples

For an example of selecting ancestor nodes, see XPathNavigator.SelectAncestors.

Remarks

The SelectAncestors method has no effect on the state of the XPathNavigator.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

SelectAncestors(String, String, Boolean)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

Selects all the ancestor nodes of the current node that have the specified local name and namespace URI.

C#
public virtual System.Xml.XPath.XPathNodeIterator SelectAncestors(string name, string namespaceURI, bool matchSelf);

Parameters

name
String

The local name of the ancestor nodes.

namespaceURI
String

The namespace URI of the ancestor nodes.

matchSelf
Boolean

To include the context node in the selection, true; otherwise, false.

Returns

An XPathNodeIterator that contains the selected nodes. The returned nodes are in reverse document order.

Exceptions

null cannot be passed as a parameter.

Examples

The following example illustrates selecting ancestor, child, and descendant nodes.

C#
XPathDocument document = new XPathDocument("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");

// Select all the descendant nodes of the book node.
XPathNodeIterator bookDescendants = navigator.SelectDescendants("", "http://www.contoso.com/books", false);

// Display the LocalName of each descendant node.
Console.WriteLine("Descendant nodes of the book node:");
while (bookDescendants.MoveNext())
{
    Console.WriteLine(bookDescendants.Current.Name);
}

// Select all the child nodes of the book node.
XPathNodeIterator bookChildren = navigator.SelectChildren("", "http://www.contoso.com/books");

// Display the LocalName of each child node.
Console.WriteLine("\nChild nodes of the book node:");
while (bookChildren.MoveNext())
{
    Console.WriteLine(bookChildren.Current.Name);
}

// Select all the ancestor nodes of the title node.
navigator.MoveToChild("title", "http://www.contoso.com/books");

XPathNodeIterator bookAncestors = navigator.SelectAncestors("", "http://www.contoso.com/books", false);

// Display the LocalName of each ancestor node.
Console.WriteLine("\nAncestor nodes of the title node:");

while (bookAncestors.MoveNext())
{
    Console.WriteLine(bookAncestors.Current.Name);
}

The example takes the contosoBooks.xml file as an input.

XML
<?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>  

Remarks

If String.Empty is specified as the name parameter, all ancestor nodes that belong to the specified namespace URI are selected. If String.Empty is specified as the namespaceURI parameter, all ancestor nodes with the specified local name that belong to no namespace are selected. If String.Empty is specified as both the local name and namespace URI, all ancestor nodes that belong to no namespace are selected.

The SelectAncestors method has no effect on the state of the XPathNavigator.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1