Share via


How to: Retrieve a Collection of Elements (LINQ to XML)

This topic demonstrates the Elements method. This method retrieves a collection of the child elements of an element.

Example

This example iterates through the child elements of the purchaseOrder element.

This example uses the following XML document: Sample XML File: Typical Purchase Order (LINQ to XML).

XElement po = XElement.Load("PurchaseOrder.xml");
IEnumerable<XElement> childElements =
    from el in po.Elements()
    select el;
foreach (XElement el in childElements)
    Console.WriteLine("Name: " + el.Name);
Dim po As XElement = XElement.Load("PurchaseOrder.xml")
Dim childElements As IEnumerable(Of XElement)
childElements = _
    From el In po.Elements() _
    Select el
For Each el As XElement In childElements
    Console.WriteLine("Name: " & el.Name.ToString())
Next

This example produces the following output.

Name: Address
Name: Address
Name: DeliveryNotes
Name: Items

See Also

Concepts

LINQ to XML Axes