Basics of LINQ to XML Programming

Source: Internet
Author: User
Tags processing instruction

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.