Export (0) Print
Expand All

Extensions.Elements(Of T) Method (IEnumerable(Of T))

Returns a collection of the child elements of every element and document in the source collection.

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

'Declaration
<ExtensionAttribute> _
Public Shared Function Elements(Of T As XContainer) ( _
	source As IEnumerable(Of T) _
) As IEnumerable(Of XElement)

Type Parameters

T

The type of the objects in source, constrained to XContainer.

Parameters

source
Type: System.Collections.Generic.IEnumerable(Of T)

An IEnumerable(Of T) of XElement that contains the source collection.

Return Value

Type: System.Collections.Generic.IEnumerable(Of XElement)
An IEnumerable(Of T) of XElement of the child elements of every element or document in the source collection.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable(Of T). When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Although Visual Basic contains an integrated elements axis that allows you to find all child elements with a specified XName for every element in the source collection, there is no integrated elements axis that allows you to retrieve a collection of every child element for every element in the source collection.

This method uses deferred execution.

The following example retrieves a collection of elements with the element name of Child. It then uses this axis method to retrieve all child elements of the collection.

Dim xmlTree As XElement = _
     <Root>
          <Child>
              <GrandChild1>1</GrandChild1>
              <GrandChild2>2</GrandChild2>
          </Child>

          <Child>
              <GrandChild3>3</GrandChild3>
              <GrandChild4>4</GrandChild4>
          </Child>

          <Child>
              <GrandChild5>5</GrandChild5>
              <GrandChild6>6</GrandChild6>
          </Child>
      </Root>

Dim allGrandChildren = From el In xmlTree.<Child>.Elements _
                       Select el

For Each el As XElement In allGrandChildren
    Console.WriteLine(el)
Next

This example produces the following output:

<GrandChild1>1</GrandChild1>
<GrandChild2>2</GrandChild2>
<GrandChild3>3</GrandChild3>
<GrandChild4>4</GrandChild4>
<GrandChild5>5</GrandChild5>
<GrandChild6>6</GrandChild6>

The following is the same example, but in this case the XML is in a namespace. For more information, see Working with XML Namespaces.

Imports <xmlns="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim xmlTree As XElement = _
             <Root>
                 <Child>
                     <GrandChild1>1</GrandChild1>
                     <GrandChild2>2</GrandChild2>
                 </Child>

                 <Child>
                     <GrandChild3>3</GrandChild3>
                     <GrandChild4>4</GrandChild4>
                 </Child>

                 <Child>
                     <GrandChild5>5</GrandChild5>
                     <GrandChild6>6</GrandChild6>
                 </Child>
             </Root>

        Dim allGrandChildren = From el In xmlTree.<Child>.Elements _
                               Select el

        For Each el As XElement In allGrandChildren
            Console.WriteLine(el)
        Next
    End Sub
End Module

This example produces the following output:

<GrandChild1 xmlns="http://www.adventure-works.com">1</GrandChild1>
<GrandChild2 xmlns="http://www.adventure-works.com">2</GrandChild2>
<GrandChild3 xmlns="http://www.adventure-works.com">3</GrandChild3>
<GrandChild4 xmlns="http://www.adventure-works.com">4</GrandChild4>
<GrandChild5 xmlns="http://www.adventure-works.com">5</GrandChild5>
<GrandChild6 xmlns="http://www.adventure-works.com">6</GrandChild6>

.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