C # Foundation Consolidation (2)-linq to XML creation XML

Source: Internet
Author: User

First, it is essential to understand what a correct XML basic format is.

1. suffix. Xml end

2. One line describes

3. There is only one root node.

:

A correct XML file can be opened and displayed by the browser. So to determine whether an XML file has errors can also be opened with the browser to see if there is an error.

Second, the traditional way of creating XML.

Namespaces: System.Xml

Class Library used: XmlDocument-Documentation

XmlElement-Elements

XmlAttribute-Properties

Code:

static void Main (string[] args) {traditionalcreatexml ();            } private static void Traditionalcreatexml () {XmlDocument Xdoc = new XmlDocument (); All elements are created using the document node (XmlDocument) 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 the Node 2 Xdoc.  AppendChild (Xele); Xdoc Add node--root node Xele. AppendChild (Xele2); Add a Node 2 (xele2) XmlAttribute xattr = Xdoc under node 1 (xele). CreateAttribute ("id");  Create attribute Xattr.value = "123"; The value of the Xele property. Attributes.append (XATTR); Insert the attribute into the node XmlText txt = xdoc.  createTextNode ("I am a text node"); Creates a text xele2. AppendChild (TXT); Inserts the text into the node Xdoc.        Save ("1.xml"); }

The key idea is to create elements, add elements , execute the above code in the Bin->debug directory to find 1.xml this file.

The contents are as follows:

Third, Linq to XML create XML

namespaces: SYSTEM.XML.LINQ;

Class Library: XDocument-Documentation

XElement-Elements

XAttribute-Properties

3.1 LINQ to XML usage

Code:

 Static voidMain (string[] args)            {Linqtoxmlcreatexml (); //traditionalcreatexml ();        }        Private Static voidLinqtoxmlcreatexml () {XDocument Xdoc=NewXDocument (); //The description will automatically create encoding for UTF-8 if you want to change to GB2312 xdeclaration dec=new xdeclaration ("1.0", "gb2312", "yes");XElement Xroot =NewXElement ("Root");//Create a nodeXElement Xele2 =NewXElement (" Person","I am the text"); XAttribute xattr=NewXAttribute ("Id","123");//Creating PropertiesXdoc. ADD (Xroot);//Adding nodesXroot.add (Xele2); Xroot.add (XATTR); //Add PropertyXdoc. Save ("2.xml"); }

Execute code, locate the 2.xml file in the Bin->debug directory

As can be seen from the above code, the LINQ to XML method is simpler and more characteristic than the traditional method.

1. Create an element and assign a value using the key/value (Key/value) Pair method

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

3.2 Real LINQ syntax

The main features of LINQ syntax are: 1. Based on functional F1 (). F2 (). f3 ().

2. Chain-type programming

Write the above 3.1 code into the LINQ syntax:

Static voidMain (string[] args)            {LinqToXMLCreateXML2 (); //Linqtoxmlcreatexml (); //traditionalcreatexml ();        }        Private Static voidLinqToXMLCreateXML2 () {NewXDocument (NewXElement ("Root",                    NewXAttribute ("Id","123"),                    NewXElement (" Person","I am a text")
) ). Save ("3.xml"); }

C # Foundation Consolidation (2)-linq to XML creation XML

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.