[转]C#操作XML的完整例子——XmlDocument篇
这是一个用c#控制台程序下, 用XmlDocument 进行XML操作的的例子,包含了查询、增加、修改、删除、保存的基本操作。较完整的描述了一个XML的整个操作流程。适合刚入门.net XML操作的朋友参考和学习。
假设有XML文件:books.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<books>
3
<book>
4
<name>哈里波特</name>
5
<price>10</price>
6
<memo>这是一本很好看的书。</memo>
7
</book>
8
<book id="B02">
9
<name>三国演义</name>
10
<price>10</price>
11
<memo>四大名著之一。</memo>
12
</book>
13
<book id="B03">
14
<name>水浒</name>
15
<price>6</price>
16
<memo>四大名著之一。</memo>
17
</book>
18
<book id="B04">
19
<name>红楼</name>
20
<price>5</price>
21
<memo>四大名著之一。</memo>
22
</book>
23
</books>
<?xml version="1.0" encoding="UTF-8"?>2
<books>3
<book>4
<name>哈里波特</name>5
<price>10</price>6
<memo>这是一本很好看的书。</memo>7
</book>8
<book id="B02">9
<name>三国演义</name>10
<price>10</price>11
<memo>四大名著之一。</memo>12
</book>13
<book id="B03">14
<name>水浒</name>15
<price>6</price>16
<memo>四大名著之一。</memo>17
</book>18
<book id="B04">19
<name>红楼</name>20
<price>5</price>21
<memo>四大名著之一。</memo>22
</book>23
</books>
下面是为Program.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Xml;
5
6
namespace TestXml
7
{
8
class Program
9
{
10
static void Main(string[] args)
11
{
12
XmlElement theBook = null, theElem = null, root = null;
13
XmlDocument xmldoc = new XmlDocument();
14
try
15
{
16
xmldoc.Load("Books.xml");
17
root = xmldoc.DocumentElement;
18
19
//--- 新建一本书开始 ----
20
theBook = xmldoc.CreateElement("book");
21
theElem = xmldoc.CreateElement("name");
22
theElem.InnerText = "新书";
23
theBook.AppendChild(theElem);
24
25
theElem = xmldoc.CreateElement("price");
26
theElem.InnerText = "20";
27
theBook.AppendChild(theElem);
28
29
theElem = xmldoc.CreateElement("memo");
30
theElem.InnerText = "新书更好看。";
31
theBook.AppendChild(theElem);
32
root.AppendChild(theBook);
33
Console.Out.WriteLine("--- 新建一本书开始 ----");
34
Console.Out.WriteLine(root.OuterXml);
35
//--- 新建一本书完成 ----
36
37
//--- 下面对《哈里波特》做一些修改。 ----
38
//--- 查询找《哈里波特》----
39
theBook = (XmlElement)root.SelectSingleNode("/books/book[name='哈里波特']");
40
Console.Out.WriteLine("--- 查找《哈里波特》 ----");
41
Console.Out.WriteLine(theBook.OuterXml);
42
//--- 此时修改这本书的价格 -----
43
theBook.GetElementsByTagName("price").Item(0).InnerText = "15";//getElementsByTagName返回的是NodeList,所以要跟上item(0)。另外,GetElementsByTagName("price")相当于SelectNodes(".//price")。
44
Console.Out.WriteLine("--- 此时修改这本书的价格 ----");
45
Console.Out.WriteLine(theBook.OuterXml);
46
//--- 另外还想加一个属性id,值为B01 ----
47
theBook.SetAttribute("id", "B01");
48
Console.Out.WriteLine("--- 另外还想加一个属性id,值为B01 ----");
49
Console.Out.WriteLine(theBook.OuterXml);
50
//--- 对《哈里波特》修改完成。 ----
51
52
//--- 再将所有价格低于10的书删除 ----
53
theBook = (XmlElement)root.SelectSingleNode("/books/book[@id='B02']");
54
Console.Out.WriteLine("--- 要用id属性删除《三国演义》这本书 ----");
55
Console.Out.WriteLine(theBook.OuterXml);
56
theBook.ParentNode.RemoveChild(theBook);
57
Console.Out.WriteLine("--- 删除后的XML ----");
58
Console.Out.WriteLine(xmldoc.OuterXml);
59
60
//--- 再将所有价格低于10的书删除 ----
61
XmlNodeList someBooks = root.SelectNodes("/books/book[price<10]");
62
Console.Out.WriteLine("--- 再将所有价格低于10的书删除 ---");
63
Console.Out.WriteLine("--- 符合条件的书有 " + someBooks.Count + "本。 ---");
64
65
for (int i = 0; i < someBooks.Count; i++)
66
{
67
someBooks.Item(i).ParentNode.RemoveChild(someBooks.Item(i));
68
}
69
Console.Out.WriteLine("--- 删除后的XML ----");
70
Console.Out.WriteLine(xmldoc.OuterXml);
71
72
xmldoc.Save("books.xml");//保存到books.xml
73
74
Console.In.Read();
75
}
76
catch (Exception e)
77
{
78
Console.Out.WriteLine(e.Message);
79
}
80
}
81
}
82
}
83
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Xml;5

6
namespace TestXml7
{8
class Program9
{10
static void Main(string[] args)11
{12
XmlElement theBook = null, theElem = null, root = null;13
XmlDocument xmldoc = new XmlDocument();14
try15
{16
xmldoc.Load("Books.xml");17
root = xmldoc.DocumentElement;18

19
//--- 新建一本书开始 ----20
theBook = xmldoc.CreateElement("book");21
theElem = xmldoc.CreateElement("name");22
theElem.InnerText = "新书";23
theBook.AppendChild(theElem);24

25
theElem = xmldoc.CreateElement("price");26
theElem.InnerText = "20";27
theBook.AppendChild(theElem);28

29
theElem = xmldoc.CreateElement("memo");30
theElem.InnerText = "新书更好看。";31
theBook.AppendChild(theElem);32
root.AppendChild(theBook);33
Console.Out.WriteLine("--- 新建一本书开始 ----");34
Console.Out.WriteLine(root.OuterXml);35
//--- 新建一本书完成 ----36

37
//--- 下面对《哈里波特》做一些修改。 ----38
//--- 查询找《哈里波特》----39
theBook = (XmlElement)root.SelectSingleNode("/books/book[name='哈里波特']");40
Console.Out.WriteLine("--- 查找《哈里波特》 ----");41
Console.Out.WriteLine(theBook.OuterXml);42
//--- 此时修改这本书的价格 -----43
theBook.GetElementsByTagName("price").Item(0).InnerText = "15";//getElementsByTagName返回的是NodeList,所以要跟上item(0)。另外,GetElementsByTagName("price")相当于SelectNodes(".//price")。44
Console.Out.WriteLine("--- 此时修改这本书的价格 ----");45
Console.Out.WriteLine(theBook.OuterXml);46
//--- 另外还想加一个属性id,值为B01 ----47
theBook.SetAttribute("id", "B01");48
Console.Out.WriteLine("--- 另外还想加一个属性id,值为B01 ----");49
Console.Out.WriteLine(theBook.OuterXml);50
//--- 对《哈里波特》修改完成。 ----51

52
//--- 再将所有价格低于10的书删除 ----53
theBook = (XmlElement)root.SelectSingleNode("/books/book[@id='B02']");54
Console.Out.WriteLine("--- 要用id属性删除《三国演义》这本书 ----");55
Console.Out.WriteLine(theBook.OuterXml);56
theBook.ParentNode.RemoveChild(theBook);57
Console.Out.WriteLine("--- 删除后的XML ----");58
Console.Out.WriteLine(xmldoc.OuterXml);59

60
//--- 再将所有价格低于10的书删除 ----61
XmlNodeList someBooks = root.SelectNodes("/books/book[price<10]");62
Console.Out.WriteLine("--- 再将所有价格低于10的书删除 ---");63
Console.Out.WriteLine("--- 符合条件的书有 " + someBooks.Count + "本。 ---");64

65
for (int i = 0; i < someBooks.Count; i++)66
{67
someBooks.Item(i).ParentNode.RemoveChild(someBooks.Item(i));68
}69
Console.Out.WriteLine("--- 删除后的XML ----");70
Console.Out.WriteLine(xmldoc.OuterXml);71

72
xmldoc.Save("books.xml");//保存到books.xml73

74
Console.In.Read();75
}76
catch (Exception e)77
{78
Console.Out.WriteLine(e.Message);79
}80
}81
}82
}83



浙公网安备 33010602011771号