XElement.DescendantsAndSelf Method

Definition

Returns a collection of elements that contain this element, and all descendant elements of this element, in document order.

Overloads

DescendantsAndSelf(XName)

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

DescendantsAndSelf()

Returns a collection of elements that contain this element, and all descendant elements of this element, in document order.

Remarks

This method uses deferred execution.

DescendantsAndSelf(XName)

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

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

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

Parameters

name
XName

The XName to match.

Returns

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

Examples

The following example creates an XML tree, and then uses this .

C#
XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "AttributeContent"),
    new XElement("Child",
        new XText("Some text"),
        new XElement("GrandChild", "element content")
    )
);
IEnumerable<XElement> das = xmlTree.DescendantsAndSelf("Child");
foreach (XElement el in das)
    Console.WriteLine(el.Name);

This example produces the following output:

Child

Remarks

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

DescendantsAndSelf()

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

Returns a collection of elements that contain this element, and all descendant elements of this element, in document order.

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

Returns

An IEnumerable<T> of XElement of elements that contain this element, and all descendant elements of this element, in document order.

Examples

The following example creates an XML tree, and then uses this .

C#
XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "AttributeContent"),
    new XElement("Child",
        new XText("Some text"),
        new XElement("GrandChild", "element content")
    )
);
IEnumerable<XElement> das =
    from el in xmlTree.DescendantsAndSelf()
    select el;
foreach (XElement el in das)
    Console.WriteLine(el.Name);

This example produces the following output:

Root
Child
GrandChild

Remarks

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