XML (2) writing XML through XmlDocument and XDocument

Source: Internet
Author: User
In. net can also be written to XML through XmlDocument and XDocument. XmlDocument is a standard XML writing class that was originally supported. now the extended XDocument class is more convenient to use, the following uses the XDocument class to write an XML file.

In. net can also be written to XML through XmlDocument and XDocument. XmlDocument is a standard XML writing class that was originally supported. now the extended XDocument class is more convenient to use, the following uses the XDocument class to write an XML file.

Write steps:

(1) first create an XDocument object

(2) set XML document definitions

(3) Create a root node

(4) Create subnodes in the cyclic list set (here the person class is used to input data)

(5) save to file

List
   
    
List = new List
    
     
(); List. add (new person () {name = "IstarI", age = 20, Email = "1061399756@qq.com"}); list. add (new person () {name = "Orange", age = 20, Email = "521@qq.com "}); // 1. create an XDocument object XDocument xDoc = new XDocument (); XDeclaration XDec = new XDeclaration ("1.0", "UTF-8", "no "); // Set the xml document definition xDoc. declaration = XDec; // 2. create the root node XElement rootElement = new XElement ("List"); xDoc. add (rootElement); // 3. create a subnode for (int I = 0; I
     
      


After writing, the file will appear in Debug, and then open it to view the desired result.

The above is the XML (2) content written to XML through XmlDocument and XDocument. For more information, see PHP Chinese website (www.php1.cn )!

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.