The Linq to Xml object model is very simple. The main classes in the System.Xml.XLinq namespace are XNode, XElement, XDocument and XAttribute. If we take out the “X” letter from the beginning of the class name, we have a hint about what each class represents in the Xml DOM. Here is the Linq to Xml class model:
XElement is the fundamental class in XLinq. XML trees are generally made up of a tree of XElements. XAttributes are name/value pairs associated with an XElement. XDocuments represents an XML document, which acts as a logical container for the XML document and they are created only if necessary, such as to hold a DTD or top level XML processing instruction (XProcessingInstruction).
XAttribute and XNode are peers and not derived from a common base class (see above). So, XML attributes are name value pairs associated with an XML element not nodes in the XML tree.
XText represents a text node that is used for creating CDATA sections or working with mixed content. As a user, you can get back the value of the text within an element or attribute as a string or other simple value.
XContainer acts as the base class for the XDocument and XElement classes and provides the core methods for querying XML data. XContainer is the only XNode that can have children, meaning either an XDocument or XElement. An XDocument can contain an XElement (the root element), an XDeclaration, an XDocumentType, or an XProcessingInstruction. An XElement can contain another XElement, an XComment, an XProcessingInstruction, and text (which can be passed in a variety of formats, but will be represented in the XML tree as text).
More on Linq to Xml: Querying XML in C# with LINQ to XML.
Related posts:
RSS feed for comments on this post · TrackBack URI
Leave a reply