Bewerken

Share via


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.

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

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.

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

Dim GC As XElement = xmlTree.<Child>.<GrandChild>(0)

Dim aas As IEnumerable(Of XElement) = _
    From el In GC.AncestorsAndSelf() _
    Select el

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

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

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.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ AncestorsAndSelf(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> AncestorsAndSelf (System.Xml.Linq.XName name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> AncestorsAndSelf (System.Xml.Linq.XName? name);
member this.AncestorsAndSelf : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>
Public Function AncestorsAndSelf (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 the ancestors of this element. Only elements that have a matching XName are included in the collection.

Examples

The following example uses this .

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

Dim GC As XElement = xmlTree.<Child>.<GrandChild>(0)
Dim aas As IEnumerable(Of XElement) = GC.AncestorsAndSelf("Child")
For Each el In aas
    Console.WriteLine(el.Name)
Next

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