XContainer.Elements Method

Definition

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

Overloads

Elements()

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

Elements(XName)

Returns a filtered collection of the child elements of this element or document, in document order. Only elements that have a matching XName are included in the collection.

Remarks

This method uses deferred execution.

Elements()

Source:
XContainer.cs
Source:
XContainer.cs
Source:
XContainer.cs

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

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements();

Returns

An IEnumerable<T> of XElement containing the child elements of this XContainer, in document order.

Examples

The following example creates an XML tree, and then selects some elements using this axis method.

C#
XElement xmlTree = new XElement("Root",  
    new XElement("Child1", 1),  
    new XElement("Child2", 2),  
    new XElement("Child3", 3),  
    new XElement("Child4", 4),  
    new XElement("Child5", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements()  
    where (int)el <= 3  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  

This example produces the following output:

<Child1>1</Child1>  
<Child2>2</Child2>  
<Child3>3</Child3>  

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

C#
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    new XElement(aw + "Child1", 1),  
    new XElement(aw + "Child2", 2),  
    new XElement(aw + "Child3", 3),  
    new XElement(aw + "Child4", 4),  
    new XElement(aw + "Child5", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements()  
    where (int)el <= 3  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  

This example produces the following output:

<aw:Child1 xmlns:aw="http://www.adventure-works.com">1</aw:Child1>  
<aw:Child2 xmlns:aw="http://www.adventure-works.com">2</aw:Child2>  
<aw:Child3 xmlns:aw="http://www.adventure-works.com">3</aw:Child3>  

Remarks

This method uses deferred execution.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Elements(XName)

Source:
XContainer.cs
Source:
XContainer.cs
Source:
XContainer.cs

Returns a filtered collection of the child elements of this element or document, in document order. Only elements that have a matching XName are included in the collection.

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements(System.Xml.Linq.XName name);
C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements(System.Xml.Linq.XName? name);

Parameters

name
XName

The XName to match.

Returns

An IEnumerable<T> of XElement containing the children of the XContainer that have a matching XName, in document order.

Examples

The following example creates an XML tree, and then selects several child elements using this axis method.

C#
XElement xmlTree = new XElement("Root",  
    new XElement("Type1", 1),  
    new XElement("Type1", 2),  
    new XElement("Type2", 3),  
    new XElement("Type2", 4),  
    new XElement("Type2", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements("Type2")  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  

This example produces the following output:

<Type2>3</Type2>  
<Type2>4</Type2>  
<Type2>5</Type2>  

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

C#
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    new XElement(aw + "Type1", 1),  
    new XElement(aw + "Type1", 2),  
    new XElement(aw + "Type2", 3),  
    new XElement(aw + "Type2", 4),  
    new XElement(aw + "Type2", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements(aw + "Type2")  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  

This example produces the following output:

<aw:Type2 xmlns:aw="http://www.adventure-works.com">3</aw:Type2>  
<aw:Type2 xmlns:aw="http://www.adventure-works.com">4</aw:Type2>  
<aw:Type2 xmlns:aw="http://www.adventure-works.com">5</aw:Type2>  

Remarks

This method uses deferred execution.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0