XNode.Ancestors Method

Definition

Returns a collection of the ancestor elements of this node.

Overloads

Ancestors()

Returns a collection of the ancestor elements of this node.

Ancestors(XName)

Returns a filtered collection of the ancestor elements of this node. Only elements that have a matching XName are included in the collection.

Remarks

Optionally a node name can be specified to filter for ancestor elements with a specific name.

The nodes in the returned collection are in reverse document order.

This method uses deferred execution.

Ancestors()

Returns a collection of the ancestor elements of this node.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Ancestors();
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors ();
member this.Ancestors : unit -> seq<System.Xml.Linq.XElement>
Public Function Ancestors () As IEnumerable(Of XElement)

Returns

An IEnumerable<T> of XElement of the ancestor elements of this node.

Examples

The following example uses this method to enumerate the ancestors of a node.

XElement xmlTree = new XElement("Root",  
    new XElement("Child",   
        new XElement("GrandChild", "content")  
    )  
);  
IEnumerable<XElement> grandChild = xmlTree.Descendants("GrandChild");  
foreach (XElement el in grandChild.Ancestors())  
    Console.WriteLine(el.Name);  
Dim xmlTree As XElement = _   
        <Root>  
            <Child>  
                <GrandChild>content</GrandChild>  
            </Child>  
        </Root>  

Dim grandChild As IEnumerable(Of XElement) = xmlTree...<GrandChild>  
For Each el In grandChild.Ancestors()  
    Console.WriteLine(el.Name)  
Next  

This example produces the following output:

Child  
Root  

Remarks

This method does not return itself in the results.

The nodes in the returned collection are in reverse document order.

This method uses deferred execution.

See also

Applies to

Ancestors(XName)

Returns a filtered collection of the ancestor elements of this node. Only elements that have a matching XName are included in the collection.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Ancestors(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors (System.Xml.Linq.XName name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors (System.Xml.Linq.XName? name);
member this.Ancestors : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>
Public Function Ancestors (name As XName) As IEnumerable(Of XElement)

Parameters

name
XName

The XName to match.

Returns

An IEnumerable<T> of XElement of the ancestor elements of this node. Only elements that have a matching XName are included in the collection.

The nodes in the returned collection are in reverse document order.

This method uses deferred execution.

Examples

The following example uses this method.

XElement xmlTree = new XElement("Root",  
    new XElement("Child",   
        new XElement("GrandChild", "content")  
    )  
);  
IEnumerable<XElement> grandChild = xmlTree.Descendants("GrandChild");  
foreach (XElement el in grandChild.Ancestors("Child"))  
    Console.WriteLine(el.Name);  
Dim xmlTree As XElement = _   
        <Root>  
            <Child>  
                <GrandChild>content</GrandChild>  
            </Child>  
        </Root>  

Dim grandChild As IEnumerable(Of XElement) = xmlTree...<GrandChild>  
For Each el In grandChild.Ancestors("Child")  
    Console.WriteLine(el.Name)  
Next  

This example produces the following output:

Child  

Remarks

This method will not return itself in the results.

See also

Applies to