LINQ to XML
The .NET Framework provides a number of APIs for working with XML
data. From Framework 3.5, the primary choice for general-purpose XML
document processing is LINQ to XML. LINQ to XML comprises a lightweight
LINQ-friendly XML document object model, and a set of supplementary
query operators. In most scenarios, it can be considered a complete
replacement for the preceding W3C-compliant DOM, a.k.a. XmlDocument.
Note
The LINQ to XML DOM is extremely well designed and highly
performant. Even without LINQ, the LINQ to XML DOM is valuable as a
lightweight facade over the low-level XmlReader and XmlWriter classes.
All LINQ to XML types are defined in the System.Xml.Linq namespace.
Architectural Overview
Consider the following XML file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?><customerid="123"status="archived"><firstname>Joe</firstname><lastname>Bloggs</lastname></customer>
As with all XML files, we start with a
declaration, and then a root
element, whose name is customer. The customer element has two
attributes, each with a name (id and status ) and value ("123" and "archived" ). Within customer, there are two child elements,
firstname and lastname, each having simple text content
("Joe" and "Bloggs" ).
Each of these constructs—declaration, element, attribute, value, and text content—can be represented with a class. And if such classes have collection properties for storing child content, we can assemble a tree of objects to fully describe a document. This is called ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access