Export (0) Print
Expand All

XNode.Ancestors Method

Returns a collection of the ancestor elements of this node.

Namespace:  System.Xml.Linq
Assemblies:   System.Xml.XDocument (in System.Xml.XDocument.dll)
  System.Xml.Linq (in System.Xml.Linq.dll)

'Declaration
Public Function Ancestors As IEnumerable(Of XElement)

Return Value

Type: System.Collections.Generic.IEnumerable(Of XElement)
An IEnumerable(Of T) of XElement of the ancestor elements of this node.

This method does not return itself in the results.

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

This method uses deferred execution.

The following example uses this method to enumerate the ancestors of a node.

Dim xmlTree As XElement = _ 
        <Root>
            <Child>
                <GrandChild>content</GrandChild>
            </Child>
        </Root>

Dim grandChild As IEnumerable(Of XElement) = xmlTree...<GrandChild>
For Each el In grandChild.Ancestors()
    Console.WriteLine(el.Name)
Next

This example produces the following output:

Child
Root

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft