XElement.AncestorsAndSelf Method

Definition

Returns a collection of elements that contain this element, and the ancestors of this element.

Overloads

AncestorsAndSelf()

Returns a collection of elements that contain this element, and the ancestors of this element.

AncestorsAndSelf(XName)

Returns a filtered collection of elements that contain this element, and the ancestors of this element. Only elements that have a matching XName are included in the collection.

Remarks

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

This method uses deferred execution.

AncestorsAndSelf()

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Returns a collection of elements that contain this element, and the ancestors of this element.

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> AncestorsAndSelf();

Returns

An IEnumerable<T> of XElement of elements that contain this element, and the ancestors of this element.

Examples

The following example creates an XML tree. It then finds the GrandChild element, and then prints the ancestors of it.

C#
XElement xmlTree = new XElement("Root",
    new XElement("Child",
        new XElement("GrandChild", "element content")
    )
);
XElement gc = xmlTree.Element("Child").Element("GrandChild");
IEnumerable<XElement> aas =
    from el in gc.AncestorsAndSelf()
    select el;
foreach (XElement el in aas)
    Console.WriteLine(el.Name);

This example produces the following output:

GrandChild
Child
Root

Remarks

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

This method uses deferred execution.

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 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 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

AncestorsAndSelf(XName)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Returns a filtered collection of elements that contain this element, and the ancestors of this element. Only elements that have a matching XName are included in the collection.

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> AncestorsAndSelf(System.Xml.Linq.XName name);
C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> AncestorsAndSelf(System.Xml.Linq.XName? name);

Parameters

name
XName

The XName to match.

Returns

An IEnumerable<T> of XElement that contain this element, and the ancestors of this element. Only elements that have a matching XName are included in the collection.

Examples

The following example uses this .

C#
XElement xmlTree = new XElement("Root",
    new XElement("Child",
        new XElement("GrandChild", "element content")
    )
);
XElement gc = xmlTree.Element("Child").Element("GrandChild");
IEnumerable<XElement> aas = gc.AncestorsAndSelf("Child");
foreach (XElement el in aas)
    Console.WriteLine(el.Name);

This example produces the following output:

Child

Remarks

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

This method uses deferred execution.

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 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 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0