編輯

共用方式為


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.

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

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 .

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);
Dim xmlTree As XElement = _
    <Root Att1="AttributeContent">
        <Child>Some text
            <GrandChild>element content</GrandChild>
        </Child>
    </Root>

Dim das As IEnumerable(Of XElement) = xmlTree.DescendantsAndSelf("Child")

For Each el In das
    Console.WriteLine(el.Name)
Next

This example produces the following output:

Child

Remarks

This method uses deferred execution.

See also

Applies to

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.

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

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 .

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);
Dim xmlTree As XElement = _
    <Root Att1="AttributeContent">
        <Child>Some text
            <GrandChild>element content</GrandChild>
        </Child>
    </Root>

Dim das As IEnumerable(Of XElement) = _
    From el In xmlTree.DescendantsAndSelf() _
    Select el

For Each el In das
    Console.WriteLine(el.Name)
Next

This example produces the following output:

Root
Child
GrandChild

Remarks

This method uses deferred execution.

See also

Applies to