1. LINQ to XML classThe System. Xml. Linq namespace contains 19 classes. The following table lists their names and descriptions:
Class DescriptionXAttribute represents an XML property XCData represents a CDATA text node XComment represents an XML annotation XContainer applies to abstract base classes of all nodes that may have child nodes XDeclaration represents an XML declaration XDocument represents an XML document XDocumentType represents an XML document type definition (DTD) XElement represents an XML Element XName represents an XML element or attribute name XNamespace represents an XML namespace XNode is an abstract class, it indicates that the XML Tree node XNodeDocumentOrderComparer provides the ability to compare the document order of nodes. XNodeEqualityComparer provides the ability to compare the value of nodes. XObject XNode and XAttribute abstract base class XObjectChange XObject triggers an event. XObjectChangeEventArgs provides data for Changing and Changed events. XProcessingInstruction indicates an XML Processing Instruction. XText indicates a text node.
The following code demonstrates how to use LINQ to XML to quickly create an xml file:
Public static void CreateDocument () {XDocument xdoc = new XDocument (new XDeclaration ("1.0", "UTF-8", "yes"), new XElement ("Root ", "root"); xdoc. save (path );}
Run this example to get an xml file with the following content:
<? Xmlversion = "1.0" encoding = "UTF-8" standalone = "yes"?> <Root> root </Root>
It can be seen that Microsoft has invested a lot of energy in LINQ, making us feel comfortable in programming. The following describes the three most used classes for processing XML: XElement, XAttribute, and XDocument. If you have mastered these classes, it will be nice to use LINQ to XML.
2. XElement classThe XElement class is one of the basic classes in LINQ to XML. It represents an XML element. You can use this class to create elements, change element content, add, change, or delete child elements, add attributes to elements, or serialize Element Content in text format. It can also interoperate with other classes in System. Xml (such as XmlReader, XmlWriter, and compiledtransform. There are many ways to create an XML document using LINQ to xml. The specific method should be based on actual needs. The simplest and most common way to create an xml document is to use the XElement class. The following code demonstrates how to use the XElement class to create an xml document:
Public static void CreateCategories () {XElement root = new XElement ("Categories", new XElement ("Ca