代码
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 //创建一个根节点(一级)
25 XmlElement root = doc.CreateElement("First");
26 doc.AppendChild(root);
27 //创建节点(二级)
28 XmlNode node = doc.CreateElement("Seconde");
29 //创建节点(三级)
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
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 //创建一个根节点(一级)
25 XmlElement root = doc.CreateElement("First");
26 doc.AppendChild(root);
27 //创建节点(二级)
28 XmlNode node = doc.CreateElement("Seconde");
29 //创建节点(三级)
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