Use C # To create XML

Source: Internet
Author: User
Code

1 Using System;
2 Using System. collections;
3 Using System. configuration;
4 Using System. Data;
5 Using System. LINQ;
6 Using System. Web;
7 Using System. Web. Security;
8 Using System. Web. UI;
9 Using System. Web. UI. htmlcontrols;
10 Using System. Web. UI. webcontrols;
11 Using System. Web. UI. webcontrols. webparts;
12 Using System. xml. LINQ;
13 Using System. xml;
14
15 Namespace Web
16 {
17 Public   Partial   Class Test: system. Web. UI. Page
18 {
19 Protected   Void Page_load ( Object Sender, eventargs E)
20 {
21 Xmldocument Doc =   New Xmldocument ();
22 Xmldeclaration Dec = Doc. createxmldeclaration ( " 1.0 " , " Gb2312 " , Null );
23 Doc. appendchild (DEC );
24 // Create a root node (level 1)
25 Xmlelement Root = Doc. createelement ( " First " );
26 Doc. appendchild (Root );
27 // Create a node (level 2)
28 Xmlnode Node = Doc. createelement ( " Seconde " );
29 // Create a node (level 3)
30 Xmlelement element1 = Doc. createelement ( " Third1 " );
31 Element1.setattribute ( " Name " , " Sam " );
32 Element1.setattribute ( " ID " , " 665 " );
33 Element1.innertext =   " Sam comment " ;
34 Node. appendchild (element1 );
35
36 Xmlelement element2 = Doc. createelement ( " Third2 " );
37 Element2.setattribute ( " Name " , " Round " );
38 Element2.setattribute ( " ID " , " 678 " );
39 Element2.innertext =   " Round comment " ;
40 Node. appendchild (element2 );
41
42 Root. appendchild (node );
43
44 Doc. Save ( @" D: \ BB. xml " );
45 Console. Write (Doc. outerxml );
46 }
47 }
48 }
49

 

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.