LINQ to XML Functional Construction for XML Trees

Functional construction in LINQ to XML provides a declarative approach to building XML documents, allowing developers to define and assemble entire nested XML trees in a single, cohesive statement. By leveraging the flexible constructors of the System.Xml.Linq classes—primarily XElement and XAttribute—developers can mirror the visual hierarchy of an XML document directly in C# code. This eliminates the tedious, multi-step procedural code required by older APIs like XmlDocument and produces cleaner, more maintainable software.

The Core Concept: The params object[] Constructor

The foundation of functional construction is the XElement constructor signature:

public XElement(XName name, params object[] content)

Because the content parameter accepts a params array of generic object types, you can pass virtually any type of data directly into an element when instantiating it. The constructor automatically handles:

Structural Mirroring

Traditional DOM construction requires creating a document, creating individual elements, setting their values, and explicitly appending each child to its parent node by node.

In contrast, functional construction mirrors the final XML structure through nested constructor calls. The indentation of the C# code matches the hierarchy of the generated XML document.

using System;
using System.Xml.Linq;

XElement purchaseOrder = new XElement("PurchaseOrder",
    new XAttribute("Id", 1001),
    new XElement("Customer",
        new XElement("Name", "Jane Doe"),
        new XElement("Email", "jane.doe@example.com")
    ),
    new XElement("Items",
        new XElement("Item",
            new XAttribute("Sku", "A123"),
            new XElement("Description", "Wireless Mouse"),
            new XElement("Price", 29.99)
        ),
        new XElement("Item",
            new XAttribute("Sku", "B456"),
            new XElement("Description", "Mechanical Keyboard"),
            new XElement("Price", 89.99)
        )
    )
);

Embedding LINQ Queries

Because the XElement constructor unwinds any IEnumerable passed to it, standard LINQ queries can be embedded directly within the tree construction. This allows in-memory collections (such as lists of objects, database results, or CSV records) to be transformed into XML inline without intermediate loops.

var products = new[]
{
    new { Sku = "A123", Name = "Wireless Mouse", Price = 29.99 },
    new { Sku = "B456", Name = "Mechanical Keyboard", Price = 89.99 }
};

XElement catalog = new XElement("Catalog",
    new XAttribute("GeneratedAt", DateTime.UtcNow),
    from p in products
    select new XElement("Product",
        new XAttribute("Sku", p.Sku),
        new XElement("Name", p.Name),
        new XElement("Price", p.Price)
    )
);

Key Advantages