Functional Construction (LINQ to XML)
LINQ to XML provides a powerful way to create XML elements called functional construction. Functional construction is the ability to create an XML tree in a single statement.
There are several key features of the LINQ to XML programming interface that enable functional construction:
The XElement constructor takes various types of arguments for content. For example, you can pass another XElement object, which becomes a child element. You can pass an XAttribute object, which becomes an attribute of the element. Or you can pass any other type of object, which is converted to a string and becomes the text content of the element.
The XElement constructor takes a params array of type Object, so that you can pass any number of objects to the constructor. This enables you to create an element that has complex content.
If an object implements IEnumerable<T>, the collection in the object is enumerated, and all items in the collection are added. If the collection contains XElement or XAttribute objects, each item in the collection is added separately. This is important because it lets you pass the results of a LINQ query to the constructor.
These features enable you to write code to create an XML tree. The following is an example:
XElement contacts =
new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144"),
new XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
)
);
These features also enable you to write code that uses the results of LINQ queries when you create an XML tree, as follows:
XElement srcTree = new XElement("Root",
new XElement("Element", 1),
new XElement("Element", 2),
new XElement("Element", 3),
new XElement("Element", 4),
new XElement("Element", 5)
);
XElement xmlTree = new XElement("Root",
new XElement("Child", 1),
new XElement("Child", 2),
from el in srcTree.Elements()
where (int)el > 2
select el
);
Console.WriteLine(xmlTree);
In Visual Basic, the same thing is accomplished through XML literals.
Dim srcTree As XElement = _
<Root>
<Element>1</Element>
<Element>2</Element>
<Element>3</Element>
<Element>4</Element>
<Element>5</Element>
</Root>
Dim xmlTree As XElement = _
<Root>
<Child>1</Child>
<Child>2</Child>
<%= From el In srcTree.Elements() _
Where CInt(el) > 2 _
Select el %>
</Root>
Console.WriteLine(xmlTree)
This example produces the following output:
<Root>
<Child>1</Child>
<Child>2</Child>
<Element>3</Element>
<Element>4</Element>
<Element>5</Element>
</Root>