Export (0) Print
Expand All

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.

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);

This example produces the following output.

Name: Address
Name: Address
Name: DeliveryNotes
Name: Items
Show:
© 2014 Microsoft