<?xml version="1.0" encoding="gb2312"?> //XML的声明
代码2 {
3 XmlDocument xmldoc;
4 XmlNode xmlnode;
5 XmlElement xmlelem;
6
7 xmldoc = new XmlDocument();
8 //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
9 XmlDeclaration xmldecl;
10 xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
11 xmldoc.AppendChild(xmldecl);
12
13 //加入一个根元素
14 xmlelem = xmldoc.CreateElement("", "rootEle", "");
15 xmldoc.AppendChild(xmlelem);
16 //加入另外一个元素
17 for (int i = 1; i < 3; i++)
18 {
19
20 XmlNode root = xmldoc.SelectSingleNode("rootEle");//查找<root>
21
22 xmlnode = xmldoc.CreateElement("Node");//创建一个<Node>节点
23
24
25 XmlElement xesub1 = xmldoc.CreateElement("ele1");
26 xesub1.SetAttribute("Attribute1", "abc");//设置该节点属性
27 xesub1.SetAttribute("Attribute2", "123");
28 xesub1.InnerText = "one";//设置文本节点
29 xmlnode.AppendChild(xesub1);//添加到<Node>节点中
30 XmlElement xesub2 = xmldoc.CreateElement("ele2");
31 xesub2.InnerText = "two";
32 xmlnode.AppendChild(xesub2);
33 XmlElement xesub3 = xmldoc.CreateElement("ele3");
34 xesub3.InnerText = "three";
35 xmlnode.AppendChild(xesub3);
36
37 root.AppendChild(xmlnode);//添加到<根>节点中
38 }
39 //保存创建好的XML文档
40 xmldoc.Save(Server.MapPath("data.xml"));
41 }
2. 加入节点
<?xml version="1.0" encoding="gb2312"?>
<rootEle>
<Node>
<ele1 Attribute1="abc" Attribute2="123">one</ele1>
<ele2>two</ele2>
<ele3>three</ele3>
</Node>
<Node>
<ele1 Attribute1="abc" Attribute2="123">one</ele1>
<ele2>two</ele2>
<ele3>three</ele3>
</Node>
<Node>
<ele1 Attribute1="newabc" Attribute2="new123">one</ele1>
<ele2>two</ele2>
<ele3>three</ele3>
</Node>
</rootEle>
代码2 {
3 XmlDocument xmldoc = new XmlDocument();
4 xmldoc.Load(Server.MapPath("data.xml"));
5 XmlNode root = xmldoc.SelectSingleNode("rootEle");//查找<rootEle>
6
7 XmlNode xmlnode = xmldoc.CreateElement("Node");//创建一个<Node>节点
8
9
10 XmlElement xesub1 = xmldoc.CreateElement("ele1");
11 xesub1.SetAttribute("Attribute1", "newabc");//设置该节点属性
12 xesub1.SetAttribute("Attribute2", "new123");
13 xesub1.InnerText = "one";//设置文本节点
14 xmlnode.AppendChild(xesub1);//添加到<Node>节点中
15 XmlElement xesub2 = xmldoc.CreateElement("ele2");
16 xesub2.InnerText = "two";
17 xmlnode.AppendChild(xesub2);
18 XmlElement xesub3 = xmldoc.CreateElement("ele3");
19 xesub3.InnerText = "three";
20 xmlnode.AppendChild(xesub3);
21
22 root.AppendChild(xmlnode);//添加到<根>节点中
23
24 xmldoc.Save(Server.MapPath("data.xml"));
25 }
3.更改节点
<?xml version="1.0" encoding="gb2312"?>
<rootEle>
<Node>
<ele1 Attribute1="abc" Attribute2="123">one</ele1>
<ele2>UpdateTwo</ele2>
<ele3>three</ele3>
</Node>
<Node>
<ele1 Attribute1="abc" Attribute2="123">one</ele1>
<ele2>UpdateTwo</ele2>
<ele3>three</ele3>
</Node>
<Node>
<ele1 Attribute1="Change" Attribute2="new123">one</ele1>
<ele2>UpdateTwo</ele2>
<ele3>three</ele3>
</Node>
</rootEle>
更改节点
4.去掉 Attribute1属性 加入flag element 删除 ele2 element
<?xml version="1.0" encoding="gb2312"?>
<rootEle>
<Node>
<ele1 Attribute2="123">one</ele1>
<ele3>three</ele3>
<flag>1</flag>
</Node>
<Node>
<ele1 Attribute2="123">one</ele1>
<ele3>three</ele3>
<flag>1</flag>
</Node>
<Node>
<ele1 Attribute2="new123">one</ele1>
<ele3>three</ele3>
<flag>1</flag>
</Node>
</rootEle>
代码2 {
3 XmlDocument xmlDoc = new XmlDocument();
4 xmlDoc.Load(Server.MapPath("data.xml"));
5
6 XmlNodeList nodeList = xmlDoc.SelectSingleNode("rootEle").ChildNodes;
7 //XmlNodeList nodeList = xmlDoc.SelectNodes("Node");
8 XmlNodeList subnodeList;
9 foreach (XmlNode xn in nodeList)
10 {
11 XmlElement xesub = xmlDoc.CreateElement("flag");
12 xesub.InnerText = "1";
13 xn.AppendChild(xesub); //加入一个节点
14
15 subnodeList = xn.ChildNodes;
16 foreach (XmlNode xn2 in subnodeList)
17 {
18 XmlElement xe = (XmlElement)xn2;
19 xe.RemoveAttribute("Attribute1");//删除Attribute1属性
20
21 if (xe.Name == "ele2")
22 {
23 xn.RemoveChild(xe);//则删除
24 }
25
26 }
27
28 }
29 xmlDoc.Save(Server.MapPath("data.xml"));//保存。
30 }

浙公网安备 33010602011771号