LINQ to XML

Namespace: System.Xml.Linq

LINQ to XML is a powerful and flexible API that allows you to query and manipulate XML documents using Language Integrated Query (LINQ) in .NET. It provides a simpler, more intuitive, and object-oriented approach to working with XML compared to traditional DOM (Document Object Model) parsing.

Introduction to LINQ to XML

LINQ to XML represents XML documents as a set of rich object-oriented classes, such as XDocument, XElement, XAttribute, and XComment. This object model allows you to traverse, query, and modify XML structures with the same ease as working with other LINQ data sources.

Key Features:

  • Object-Oriented Model: Represents XML nodes as objects, making manipulation intuitive.
  • LINQ Integration: Leverages the full power of LINQ for querying XML.
  • Functional Style: Supports a functional programming style for XML transformations.
  • Simplified API: More straightforward than traditional DOM manipulation.
  • Projection: Easily transform XML structures into custom objects.

Core Classes

The primary classes in LINQ to XML are:

  • XDocument: Represents an entire XML document, including declarations and processing instructions.
  • XElement: Represents an XML element. This is the most commonly used class.
  • XAttribute: Represents an attribute of an XML element.
  • XName: Represents the qualified name of an element or attribute.
  • XNamespace: Represents an XML namespace.
  • XNode: The abstract base class for all node types in an XML tree.
  • XComment: Represents an XML comment.
  • XText: Represents text content within an XML element.

Basic Operations

Loading and Creating XML

You can load an existing XML file or create a new XML document from scratch:


// Loading from a file
XDocument docFromFile = XDocument.Load("data.xml");

// Creating a new document programmatically
XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XElement("Root",
        new XElement("Child1", "Some text"),
        new XElement("Child2",
            new XAttribute("id", "123"),
            new XElement("Grandchild", "More text")
        )
    )
);

// Save the document
doc.Save("output.xml");
                

Querying XML

Use LINQ queries to extract data from your XML structure:


// Assuming 'doc' is an XDocument loaded or created
var elements = from el in doc.Descendants("Child1")
               select el.Value;

foreach (var text in elements)
{
    Console.WriteLine(text);
}

// Querying elements with attributes
var items = from item in doc.Root.Elements("Item")
            where (string)item.Attribute("status") == "active"
            select new
            {
                Name = item.Element("Name").Value,
                Price = (decimal)item.Element("Price")
            };

foreach (var item in items)
{
    Console.WriteLine($"{item.Name}: ${item.Price}");
}
                

Modifying XML

LINQ to XML makes it easy to add, remove, or change elements and attributes:


// Add a new element
doc.Root.Add(new XElement("NewElement", "This is new."));

// Add an attribute to an existing element
XElement existingElement = doc.Root.Element("Child2");
if (existingElement != null)
{
    existingElement.SetAttributeValue("version", "2.0");
}

// Remove an element
doc.Root.Element("Child1")?.Remove();

// Update element value
XElement elementToUpdate = doc.Descendants("Grandchild").FirstOrDefault();
if (elementToUpdate != null)
{
    elementToUpdate.Value = "Updated grandchild text.";
}
                

LINQ to XML vs. DOM

LINQ to XML offers several advantages over the traditional DOM approach:

  • Simplicity: The object model is more intuitive and easier to navigate.
  • Performance: Can be more memory-efficient for large XML documents, especially when using XDocument.Load() without loading the entire document into memory for modification.
  • LINQ Power: Seamless integration with LINQ allows for powerful, declarative querying.
  • Functional Transformations: Easier to perform transformations and projections.
Note: While LINQ to XML is excellent for most XML manipulation tasks, for extremely high-performance, low-level XML processing, or when dealing with very large XML files where memory is a critical concern, consider using the System.Xml.XmlReader and System.Xml.XmlWriter classes for a stream-based approach.

Further Reading