Export (0) Print
Expand All

XContainer.Descendants Method

Returns a collection of the descendant elements for this document or element, in document order.

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

'Declaration
Public Function Descendants As IEnumerable(Of XElement)

Return Value

Type: System.Collections.Generic.IEnumerable(Of XElement)
An IEnumerable(Of T) of XElement containing the descendant elements of the XContainer.

Note that this method will not return itself in the resulting IEnumerable(Of T). See DescendantsAndSelf if you need to include the current XElement in the results.

This method uses deferred execution.

The following example creates an XML tree, and then uses this axis method to retrieve the descendants.

' Attributes are not nodes, so will not be returned by DescendantNodes.
Dim xmlTree As XElement = _
    <Root Att1="AttributeContent">
        <Child>Some text
            <GrandChild>element content</GrandChild>
        </Child>
    </Root>
Dim de = From el In xmlTree.Descendants _
         Select el

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

This example produces the following output:

Child
GrandChild

.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