LINQ to XML is a memory-based XML programming interface that enables LINQ. It can be used to process XML in. NET Framework programming languages.
It adds the XML documentIn MemoryThis is similar to the Document Object Model (DOM ). You can query and modify an XML document. After modification, you can save it as a file, serialize it, and send it over the network. However, unlike Dom, LINQ to XML provides a new object model, which is a lightweight model and easier to use, this model utilizes Visual C #2008 language improvements.
The most important advantage of LINQ to XML is its integration with language-Integrated Query (LINQ. Because of this integration, you can write queries for memory XML documents to retrieve the set of elements and attributes. The query function of LINQ to XML is functionally (though not syntactically) comparable to that of xpath and XQuery. After Visual C #2008 is integrated with LINQ, it can provide enhanced typing functions, checking during compilation, and improved debugger support.
By using the query results as parameters of the xelement and xattribute object constructors, a powerful method for creating an XML tree is provided. This method is calledFunction constructionUsing this method, developers can easily convert the XML tree from one shape to another.
LINQ to XML provides improved XML programming interfaces, which may be as important as the LINQ function of LINQ to XML. You can implement any expected operations during XML programming through LINQ to XML, including:
Load XML from a file or stream.
Serialize XML into a file or stream.
Create XML from the beginning using function construction.
Use an XPATH-like axis to query XML.
Use , , And And other methods to operate the memory XML tree.
Use XSD to verify the XML tree.
Using a combination of these features, you can convert an XML tree from one shape to another.
It is important to create an XML tree easily. For example, to create a small XML tree, you can write the following C # code:
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") ) ) );
Note that the code used to construct the XML tree shows the structure of the basic XML.
Directly use XML elements
When using XML programming, the main focus is on XML elements and attributes. The XML elements and attributes can be directly used in LINQ to XML. For example, you can perform the following operations:
Create XML elements without using document objects. This simplifies programming when the XML tree fragment is required.
Load directly from XML fileT: system. xml. LINQ. xelementObject.
SetT: system. xml. LINQ. xelementObject serialization is a file or stream.
The xdocument class is used only when you add comments or process instructions at the root level of a document when using LINQ to XML.
Simplified processing of names and namespaces
Processing names, namespaces, and namespaces prefixes are usually complex parts of XML programming. The namespace prefix does not need to be processed at all for LINQ to XML, which simplifies the name and namespace. You can easily control the namespace prefix. However, if you decide not to explicitly control the namespace prefix, during serialization, LINQ to XML will allocate the namespace prefix (if needed) or use the default namespace for serialization. If the default namespace is used, the generated document does not have a namespace prefix.
You do not need to understand what nametable is and how it is used.