Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Extensions.Elements<T> Method (IEnumerable<T>)

 

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

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

public static IEnumerable<XElement> Elements<T>(
	this IEnumerable<T> source
)
where T : XContainer

Parameters

source
Type: System.Collections.Generic.IEnumerable<T>

An IEnumerable<T> of XElement that contains the source collection.

Return Value

Type: System.Collections.Generic.IEnumerable<XElement>

An IEnumerable<T> of XElement of the child elements of every element or document in the source collection.

Type Parameters

T

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

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.


                XElement xmlTree = new XElement("Root",
    new XElement("Child",
        new XElement("GrandChild1", 1),
        new XElement("GrandChild2", 2)
    ),
    new XElement("Child",
        new XElement("GrandChild3", 3),
        new XElement("GrandChild4", 4)
    ),
    new XElement("Child",
        new XElement("GrandChild5", 5),
        new XElement("GrandChild6", 6)
    )
);

IEnumerable<XElement> allGrandChildren =
    from el in xmlTree.Elements("Child").Elements()
    select el;

foreach (XElement el in allGrandChildren)
    Console.WriteLine(el);

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.


                XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = new XElement(aw + "Root",
    new XElement(aw + "Child",
        new XElement(aw + "GrandChild1", 1),
        new XElement(aw + "GrandChild2", 2)
    ),
    new XElement(aw + "Child",
        new XElement(aw + "GrandChild3", 3),
        new XElement(aw + "GrandChild4", 4)
    ),
    new XElement(aw + "Child",
        new XElement(aw + "GrandChild5", 5),
        new XElement(aw + "GrandChild6", 6)
    )
);

IEnumerable<XElement> allGrandChildren =
    from el in xmlTree.Elements(aw + "Child").Elements()
    select el;

foreach (XElement el in allGrandChildren)
    Console.WriteLine(el);

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>

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft