LINQ to XML vs. DOM
This section describes some key differences between LINQ to XML and the current predominant XML programming API, the W3C Document Object Model (DOM).
In the W3C DOM, you build an XML tree from the bottom up; that is, you create a document, you create elements, and then you add the elements to the document.
For example, the following would be a typical way to create an XML tree using the Microsoft implementation of DOM, XmlDocument:
XmlDocument doc = new XmlDocument(); XmlElement name = doc.CreateElement("Name"); name.InnerText = "Patrick Hines"; XmlElement phone1 = doc.CreateElement("Phone"); phone1.SetAttribute("Type", "Home"); phone1.InnerText = "206-555-0144"; XmlElement phone2 = doc.CreateElement("Phone"); phone2.SetAttribute("Type", "Work"); phone2.InnerText = "425-555-0145"; XmlElement street1 = doc.CreateElement("Street1"); street1.InnerText = "123 Main St"; XmlElement city = doc.CreateElement("City"); city.InnerText = "Mercer Island"; XmlElement state = doc.CreateElement("State"); state.InnerText = "WA"; XmlElement postal = doc.CreateElement("Postal"); postal.InnerText = "68042"; XmlElement address = doc.CreateElement("Address"); address.AppendChild(street1); address.AppendChild(city); address.AppendChild(state); address.AppendChild(postal); XmlElement contact = doc.CreateElement("Contact"); contact.AppendChild(name); contact.AppendChild(phone1); contact.AppendChild(phone2); contact.AppendChild(address); XmlElement contacts = doc.CreateElement("Contacts"); contacts.AppendChild(contact); doc.AppendChild(contacts);
This style of coding does not visually provide much information about the structure of the XML tree. LINQ to XML supports this approach to constructing an XML tree, but also supports an alternative approach, functional construction. Functional construction uses the XElement and XAttribute constructors to build an XML tree.
Here is how you would construct the same XML tree by using LINQ to XML functional construction:
XElement contacts = new XElement("Contacts", new XElement("Contact", new XElement("Name", "Patrick Hines"), new XElement("Phone", "206-555-0144", new XAttribute("Type", "Home")), new XElement("phone", "425-555-0145", new XAttribute("Type", "Work")), new XElement("Address", new XElement("Street1", "123 Main St"), new XElement("City", "Mercer Island"), new XElement("State", "WA"), new XElement("Postal", "68042") ) ) );
Notice that indenting the code to construct the XML tree shows the structure of the underlying XML.
In Visual Basic, the code to construct the XML tree is even simpler, because it uses an XML literal:
Dim contacts = _ <Contacts> <Contact> <Name>Patrick Hines</Name> <Phone Type="Home">206-555-0144</Phone> <Phone Type="Work">425-555-0145</Phone> <Address> <Street1>123 Main St</Street1> <City>Mercer Island</City> <State>WA</State> <Postal>68042</Postal> </Address> </Contact> </Contacts>
If you have an existing XML document and you want to create an XML tree from it, you can open the XML document in an editor, copy the XML to the clipboard, open a Visual Basic module in Visual Studio, and paste the XML directly into the Visual Basic code editor.
For more information, see Creating XML Trees.
When you program with XML, your primary focus is usually on XML elements and perhaps on attributes. In LINQ to XML, you can work directly with XML elements and attributes. For example, you can do the following:
Create XML elements without using a document object at all. This simplifies programming when you have to work with fragments of XML trees.
Load T:System.Xml.Linq.XElement objects directly from an XML file.
Serialize T:System.Xml.Linq.XElement objects to a file or a stream.
Compare this to the W3C DOM, in which the XML document is used as a logical container for the XML tree. In DOM, XML nodes, including elements and attributes, must be created in the context of an XML document. Here is a fragment of the code to create a name element in DOM:
XmlDocument doc = new XmlDocument(); XmlElement name = doc.CreateElement("Name"); name.InnerText = "Patrick Hines"; doc.AppendChild(name);
If you want to use an element across multiple documents, you must import the nodes across documents. LINQ to XML avoids this layer of complexity.
When using LINQ to XML, you use the XDocument class only if you want to add a comment or processing instruction at the root level of the document.
Handling names, namespaces, and namespace prefixes is generally a complex part of XML programming. LINQ to XML simplifies names and namespaces by eliminating the requirement to deal with namespace prefixes. If you want to control namespace prefixes, you can. But if you decide to not explicitly control namespace prefixes, LINQ to XML will assign namespace prefixes during serialization if they are required, or will serialize using default namespaces if they are not. If default namespaces are used, there will be no namespace prefixes in the resulting document. For more information, see Working with XML Namespaces.
Another problem with the DOM is that it does not let you change the name of a node. Instead, you have to create a new node and copy all the child nodes to it, losing the original node identity. LINQ to XML avoids this problem by enabling you to set the XName property on a node.
LINQ to XML lets you load XML by using static methods, instead of instance methods. This simplifies loading and parsing. For more information, see How to: Load XML from a File.
LINQ to XML further simplifies XML programming by removing support for entities and entity references. The management of entities is complex, and is rarely used. Removing their support increases performance and simplifies the programming interface. When a LINQ to XML tree is populated, all DTD entities are expanded.
LINQ to XML handles white space more simply than the DOM.
A common scenario is to read indented XML, create an in-memory XML tree without any white space text nodes (that is, not preserving white space), perform some operations on the XML, and then save the XML with indentation. When you serialize the XML with formatting, only significant white space in the XML tree is preserved. This is the default behavior for LINQ to XML.
Another common scenario is to read and modify XML that has already been intentionally indented. You might not want to change this indentation in any way. In LINQ to XML, you can do this by preserving white space when you load or parse the XML and disabling formatting when you serialize the XML.
LINQ to XML stores white space as an XText node, instead of having a specialized Whitespace node type, as the DOM does.
LINQ to XML elements support an extensible set of annotations. This is useful for tracking miscellaneous information about an element, such as schema information, information about whether the element is bound to a UI, or any other kind of application-specific information. For more information, see LINQ to XML Annotations.
LINQ to XML provides support for XSD validation through extension methods in the System.Xml.Schema namespace. You can validate that an XML tree complies with an XSD. You can populate the XML tree with the post-schema-validation infoset (PSVI). For more information, see How to: Validate Using XSD (LINQ to XML) and Extensions.