Прочетете на английски Редактиране

Споделяне чрез


XElement.DescendantNodesAndSelf Method

Definition

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

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XNode> DescendantNodesAndSelf();

Returns

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

Examples

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

C#
XElement xmlTree = new XElement("Root",
    // Attributes are not nodes, so will not be returned by DescendantNodesAndSelf.
    new XAttribute("Att1", "AttributeContent"),
    new XElement("Child",
        new XText("Some text"),
        new XElement("GrandChild", "element content")
    )
);
IEnumerable<XNode> dnas =
    from node in xmlTree.DescendantNodesAndSelf()
    select node;
foreach (XNode node in dnas)
{
    if (node is XElement)
        Console.WriteLine((node as XElement).Name);
    else
        Console.WriteLine(node);
}

This example produces the following output:

Root
Child
Some text
GrandChild
element content

Remarks

This method uses deferred execution.

Applies to

Продукт Версии
.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

See also