C # basic consolidation (2)-create XML by using Linq To XML,

Source: Internet
Author: User

C # basic consolidation (2)-create XML by using Linq To XML,
1. First, you must know what the basic XML format is.

1. End of suffix. xml

2. There is a line of Description

3. There is only one root node.

:

A correct xml file can be opened and displayed by the browser. You can use a browser to check whether an xml file has any errors.

2. Traditional XML creation methods.

Namespace: System. XML

Library used: XmlDocument-document

XmlElement-Element

XmlAttribute-attribute

Code:

Static void Main (string [] args) {TraditionalCreateXML ();} private static void TraditionalCreateXML () {XmlDocument xdoc = new XmlDocument (); // all elements use the document node (XmlDocument) to create XmlDeclaration xdec = xdoc. createXmlDeclaration ("1.0", "gb2312", null); // xml Description xdoc. appendChild (xdec); // Add description XmlElement xele = xdoc. createElement ("root"); // create node 1 XmlElement xele2 = xdoc. createElement ("person"); // create node 2 xdoc. appendChild (xele); // Add node-root node xele. appendChild (xele2); // Add a node 2 (xele2) XmlAttribute xAttr = xdoc under Node 1 (xele. createAttribute ("id"); // create the property xAttr. value = "123"; // attribute Value xele. attributes. append (xAttr); // insert attributes to the node XmlText txt = xdoc. createTextNode ("I Am a text node"); // create a text xele2.AppendChild (txt); // Insert the text to the node xdoc. save ("1.xml ");}

The key idea is:Create element> Add ElementAfter running the above Code, find the 1. xml file in the bin-> debug directory.

The content is as follows:

3. Create XML by using Linq To XML

Namespace: System. XML. Linq;

Class Library: XDocument-Documentation

XElement-Element

XAttribute-attribute

3.1 usage of Linq To XML

Code:

Static void Main (string [] args) {LinqToXMLCreateXML (); // TraditionalCreateXML ();} private static void LinqToXMLCreateXML () {XDocument xdoc = new XDocument (); // description will automatically create Encoding As UTF-8 if you want to change to XDeclaration dec = new XDeclaration ("1.0", "GB2312", "yes") for gb2312 "); XElement xRoot = new XElement ("root"); // create a node XElement xele2 = new XElement ("person", "I am text "); XAttribute xAttr = new XAttribute ("Id", "123"); // create an attribute xdoc. add (xRoot); // Add node xRoot. add (xele2); xRoot. add (xAttr); // Add property xdoc. save ("2.xml ");}

Run the code and find the 2. xml file in the bin-> debug directory.

From the code above, we can see that the Linq to xml method is simpler than the traditional method and has its own characteristics.

1. When creating an element, you can use a key/value pair to create an element and assign a value.

2. When adding elements or attributes, the Add () method is used.

3.2 real linq syntax

Main features of the linq syntax: 1. Based on the functional formula f1 (). f2 (). f3 ().....

2. Chain Programming

Write the Code 3.1 above as the linq syntax:

Static void Main (string [] args) {LinqToXMLCreateXML2 (); // LinqToXMLCreateXML (); // TraditionalCreateXML ();} private static void LinqToXMLCreateXML2 () {new XDocument (new XElement ("root", new XAttribute ("Id", "123"), new XElement ("person", "I am a text ")
). Save ("3.xml ");}

 

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.