Ancestors Method
Collapse the table of content
Expand the table of content

XNode.Ancestors Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Returns a collection of the ancestor elements of this node.

Namespace:  System.Xml.Linq
Assembly:  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 output As New StringBuilder
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()
    output.Append(el.Name)
    output.Append(Environment.NewLine)
Next

OutputTextBlock.Text = output.ToString()


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft