C#操作xml文件入门
1
Posted on 2006-10-10 17:10 小隐任行 阅读(305) 评论(0) 编辑 收藏 所属分类: 编程资料
2
已知有一个XML文件(bookstore.xml)如下:
3
4
5
<?xml version="1.0" encoding="gb2312"?>
6
<bookstore>
7
<book genre="fantasy" ISBN="2-3631-4">
8
<title>Oberon's Legacy</title>
9
<author>Corets, Eva</author>
10
<price>5.95</price>
11
</book>
12
</bookstore>
13
14
15
16
17
18
1、往<bookstore>节点中插入一个<book>节点:
19
20
21
22
23
XmlDocument xmlDoc=new XmlDocument();
24
xmlDoc.Load("bookstore.xml");
25
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
26
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
27
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
28
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
29
30
XmlElement xesub1=xmlDoc.CreateElement("title");
31
xesub1.InnerText="CS从入门到精通";//设置文本节点
32
xe1.AppendChild(xesub1);//添加到<book>节点中
33
XmlElement xesub2=xmlDoc.CreateElement("author");
34
xesub2.InnerText="候捷";
35
xe1.AppendChild(xesub2);
36
XmlElement xesub3=xmlDoc.CreateElement("price");
37
xesub3.InnerText="58.3";
38
xe1.AppendChild(xesub3);
39
40
root.AppendChild(xe1);//添加到<bookstore>节点中
41
xmlDoc.Save("bookstore.xml");
42
43
44
45
46
47
//================
48
结果为:
49
50
51
52
53
<?xml version="1.0" encoding="gb2312"?>
54
<bookstore>
55
<book genre="fantasy" ISBN="2-3631-4">
56
<title>Oberon's Legacy</title>
57
<author>Corets, Eva</author>
58
<price>5.95</price>
59
</book>
60
<book genre="李赞红" ISBN="2-3631-4">
61
<title>CS从入门到精通</title>
62
<author>候捷</author>
63
<price>58.3</price>
64
</book>
65
</bookstore>
66
67
68
69
70
71
2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
72
73
74
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
75
foreach(XmlNode xn in nodeList)//遍历所有子节点
76
{
77
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
78
if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
79
{
80
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”
81
82
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
83
foreach(XmlNode xn1 in nls)//遍历
84
{
85
XmlElement xe2=(XmlElement)xn1;//转换类型
86
if(xe2.Name=="author")//如果找到
87
{
88
xe2.InnerText="亚胜";//则修改
89
break;//找到退出来就可以了
90
}
91
}
92
break;
93
}
94
}
95
96
xmlDoc.Save("bookstore.xml");//保存。
97
98
99
100
101
//=================
102
103
最后结果为:
104
105
106
<?xml version="1.0" encoding="gb2312"?>
107
<bookstore>
108
<book genre="fantasy" ISBN="2-3631-4">
109
<title>Oberon's Legacy</title>
110
<author>Corets, Eva</author>
111
<price>5.95</price>
112
</book>
113
<book genre="update李赞红" ISBN="2-3631-4">
114
<title>CS从入门到精通</title>
115
<author>亚胜</author>
116
<price>58.3</price>
117
</book>
118
</bookstore>
119
120
121
122
123
3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。
124
125
126
XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
127
128
foreach(XmlNode xn in xnl)
129
{
130
XmlElement xe=(XmlElement)xn;
131
132
133
if(xe.GetAttribute("genre")=="fantasy")
134
{
135
xe.RemoveAttribute("genre");//删除genre属性
136
}
137
else if(xe.GetAttribute("genre")=="update李赞红")
138
{
139
xe.RemoveAll();//删除该节点的全部内容
140
}
141
}
142
xmlDoc.Save("bookstore.xml");
143
144
//====================
145
146
最后结果为:
147
148
149
<?xml version="1.0" encoding="gb2312"?>
150
<bookstore>
151
<book ISBN="2-3631-4">
152
<title>Oberon's Legacy</title>
153
<author>Corets, Eva</author>
154
<price>5.95</price>
155
</book>
156
<book>
157
</book>
158
</bookstore>
159
160
161
4、显示所有数据。
162
163
164
XmlNode xn=xmlDoc.SelectSingleNode("bookstore");
165
166
XmlNodeList xnl=xn.ChildNodes;
167
168
foreach(XmlNode xnf in xnl)
169
{
170
XmlElement xe=(XmlElement)xnf;
171
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
172
Console.WriteLine(xe.GetAttribute("ISBN"));
173
174
XmlNodeList xnf1=xe.ChildNodes;
175
foreach(XmlNode xn2 in xnf1)
176
{
177
Console.WriteLine(xn2.InnerText);//显示子节点点文本
178
}
179
}
180
Posted on 2006-10-10 17:10 小隐任行 阅读(305) 评论(0) 编辑 收藏 所属分类: 编程资料 2
已知有一个XML文件(bookstore.xml)如下:3

4

5
<?xml version="1.0" encoding="gb2312"?>6
<bookstore>7
<book genre="fantasy" ISBN="2-3631-4">8
<title>Oberon's Legacy</title>9
<author>Corets, Eva</author>10
<price>5.95</price>11
</book>12
</bookstore>13

14

15

16

17

18
1、往<bookstore>节点中插入一个<book>节点:19

20

21

22

23
XmlDocument xmlDoc=new XmlDocument();24
xmlDoc.Load("bookstore.xml");25
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>26
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点27
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性28
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性29

30
XmlElement xesub1=xmlDoc.CreateElement("title");31
xesub1.InnerText="CS从入门到精通";//设置文本节点32
xe1.AppendChild(xesub1);//添加到<book>节点中33
XmlElement xesub2=xmlDoc.CreateElement("author");34
xesub2.InnerText="候捷";35
xe1.AppendChild(xesub2);36
XmlElement xesub3=xmlDoc.CreateElement("price");37
xesub3.InnerText="58.3";38
xe1.AppendChild(xesub3);39

40
root.AppendChild(xe1);//添加到<bookstore>节点中41
xmlDoc.Save("bookstore.xml");42

43

44

45

46

47
//================48
结果为:49

50

51

52

53
<?xml version="1.0" encoding="gb2312"?>54
<bookstore>55
<book genre="fantasy" ISBN="2-3631-4">56
<title>Oberon's Legacy</title>57
<author>Corets, Eva</author>58
<price>5.95</price>59
</book>60
<book genre="李赞红" ISBN="2-3631-4">61
<title>CS从入门到精通</title>62
<author>候捷</author>63
<price>58.3</price>64
</book>65
</bookstore>66

67

68

69

70

71
2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。72

73

74
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点75
foreach(XmlNode xn in nodeList)//遍历所有子节点76
{77
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型78
if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”79
{80
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”81

82
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点83
foreach(XmlNode xn1 in nls)//遍历84
{85
XmlElement xe2=(XmlElement)xn1;//转换类型86
if(xe2.Name=="author")//如果找到87
{88
xe2.InnerText="亚胜";//则修改89
break;//找到退出来就可以了90
}91
}92
break;93
}94
}95

96
xmlDoc.Save("bookstore.xml");//保存。97

98

99

100

101
//=================102

103
最后结果为:104

105

106
<?xml version="1.0" encoding="gb2312"?>107
<bookstore>108
<book genre="fantasy" ISBN="2-3631-4">109
<title>Oberon's Legacy</title>110
<author>Corets, Eva</author>111
<price>5.95</price>112
</book>113
<book genre="update李赞红" ISBN="2-3631-4">114
<title>CS从入门到精通</title>115
<author>亚胜</author>116
<price>58.3</price>117
</book>118
</bookstore>119

120

121

122

123
3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。124

125

126
XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;127

128
foreach(XmlNode xn in xnl)129
{130
XmlElement xe=(XmlElement)xn;131

132

133
if(xe.GetAttribute("genre")=="fantasy")134
{135
xe.RemoveAttribute("genre");//删除genre属性136
}137
else if(xe.GetAttribute("genre")=="update李赞红")138
{139
xe.RemoveAll();//删除该节点的全部内容140
}141
}142
xmlDoc.Save("bookstore.xml");143

144
//====================145

146
最后结果为:147

148

149
<?xml version="1.0" encoding="gb2312"?>150
<bookstore>151
<book ISBN="2-3631-4">152
<title>Oberon's Legacy</title>153
<author>Corets, Eva</author>154
<price>5.95</price>155
</book>156
<book>157
</book>158
</bookstore> 159

160

161
4、显示所有数据。162

163

164
XmlNode xn=xmlDoc.SelectSingleNode("bookstore");165

166
XmlNodeList xnl=xn.ChildNodes;167

168
foreach(XmlNode xnf in xnl)169
{170
XmlElement xe=(XmlElement)xnf;171
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值172
Console.WriteLine(xe.GetAttribute("ISBN"));173

174
XmlNodeList xnf1=xe.ChildNodes;175
foreach(XmlNode xn2 in xnf1)176
{177
Console.WriteLine(xn2.InnerText);//显示子节点点文本178
}179
} 180

浙公网安备 33010602011771号