1
在C#.net中如何操作XML
2
需要添加的命名空间:
3
using System.Xml;
4
5
定义几个公共对象:
6
XmlDocument xmldoc ;
7
XmlNode xmlnode ;
8
XmlElement xmlelem ;
9
10
1,创建到服务器同名目录下的xml文件:
11
12
13
方法一:
14
xmldoc = new XmlDocument ( ) ;
15
//加入XML的声明段落
16
xmlnode = xmldoc.CreateNode ( XmlNodeType.XmlDeclaration , "" , "" ) ;
17
xmldoc.AppendChild ( xmlnode ) ;
18
//加入一个根元素
19
xmlelem = xmldoc.CreateElement ( "" , "Employees" , "" ) ;
20
xmldoc.AppendChild ( xmlelem ) ;
21
//加入另外一个元素
22
for(int i=1;i<3;i++)
23

{
24
25
XmlNode root=xmldoc.SelectSingleNode("Employees");//查找<Employees>
26
XmlElement xe1=xmldoc.CreateElement("Node");//创建一个<Node>节点
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);//添加到<Node>节点中
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);//添加到<Employees>节点中
41
}
42
//保存创建好的XML文档
43
xmldoc.Save ( Server.MapPath("data.xml") ) ;
44
45
/**///////////////////////////////////////////////////////////////////////////////////////
46
结果:在同名目录下生成了名为data.xml的文件,内容如下,
47
<?xml version="1.0"?>
48
<Employees>
49
<Node genre="李赞红" ISBN="2-3631-4">
50
<title>CS从入门到精通</title>
51
<author>候捷</author>
52
<price>58.3</price>
53
</Node>
54
<Node genre="李赞红" ISBN="2-3631-4">
55
<title>CS从入门到精通</title>
56
<author>候捷</author>
57
<price>58.3</price>
58
</Node>
59
</Employees>
60
61
62
方法二:
63
XmlTextWriter xmlWriter;
64
string strFilename = Server.MapPath("data1.xml") ;
65
66
xmlWriter = new XmlTextWriter(strFilename,Encoding.Default);//创建一个xml文档
67
xmlWriter.Formatting = Formatting.Indented;
68
xmlWriter.WriteStartDocument();
69
xmlWriter.WriteStartElement("Employees");
70
71
xmlWriter.WriteStartElement("Node");
72
xmlWriter.WriteAttributeString("genre","李赞红");
73
xmlWriter.WriteAttributeString("ISBN","2-3631-4");
74
75
xmlWriter.WriteStartElement("title");
76
xmlWriter.WriteString("CS从入门到精通");
77
xmlWriter.WriteEndElement();
78
79
xmlWriter.WriteStartElement("author");
80
xmlWriter.WriteString("候捷");
81
xmlWriter.WriteEndElement();
82
83
xmlWriter.WriteStartElement("price");
84
xmlWriter.WriteString("58.3");
85
xmlWriter.WriteEndElement();
86
87
xmlWriter.WriteEndElement();
88
89
xmlWriter.Close();
90
/**///////////////////////////////////////////////////////////////////////////////////////
91
结果:
92
<?xml version="1.0" encoding="gb2312"?>
93
<Employees>
94
<Node genre="李赞红" ISBN="2-3631-4">
95
<title>CS从入门到精通</title>
96
<author>候捷</author>
97
<price>58.3</price>
98
</Node>
99
</Employees>
100
101
2,添加一个结点:
102
103
XmlDocument xmlDoc=new XmlDocument();
104
xmlDoc.Load(Server.MapPath("data.xml"));
105
XmlNode root=xmlDoc.SelectSingleNode("Employees");//查找<Employees>
106
XmlElement xe1=xmlDoc.CreateElement("Node");//创建一个<Node>节点
107
xe1.SetAttribute("genre","张三");//设置该节点genre属性
108
xe1.SetAttribute("ISBN","1-1111-1");//设置该节点ISBN属性
109
110
XmlElement xesub1=xmlDoc.CreateElement("title");
111
xesub1.InnerText="C#入门帮助";//设置文本节点
112
xe1.AppendChild(xesub1);//添加到<Node>节点中
113
XmlElement xesub2=xmlDoc.CreateElement("author");
114
xesub2.InnerText="高手";
115
xe1.AppendChild(xesub2);
116
XmlElement xesub3=xmlDoc.CreateElement("price");
117
xesub3.InnerText="158.3";
118
xe1.AppendChild(xesub3);
119
120
root.AppendChild(xe1);//添加到<Employees>节点中
121
xmlDoc.Save ( Server.MapPath("data.xml") );
122
123
/**///////////////////////////////////////////////////////////////////////////////////////
124
结果:在xml原有的内容里添加了一个结点,内容如下,
125
<?xml version="1.0"?>
126
<Employees>
127
<Node genre="李赞红" ISBN="2-3631-4">
128
<title>CS从入门到精通</title>
129
<author>候捷</author>
130
<price>58.3</price>
131
</Node>
132
<Node genre="李赞红" ISBN="2-3631-4">
133
<title>CS从入门到精通</title>
134
<author>候捷</author>
135
<price>58.3</price>
136
</Node>
137
<Node genre="张三" ISBN="1-1111-1">
138
<title>C#入门帮助</title>
139
<author>高手</author>
140
<price>158.3</price>
141
</Node>
142
</Employees>
143
144
3,修改结点的值(属性和子结点):
145
146
XmlDocument xmlDoc=new XmlDocument();
147
xmlDoc.Load( Server.MapPath("data.xml") );
148
149
XmlNodeList nodeList=xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点
150
151
foreach(XmlNode xn in nodeList)//遍历所有子节点
152

{
153
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
154
if(xe.GetAttribute("genre")=="张三")//如果genre属性值为“张三”
155

{
156
xe.SetAttribute("genre","update张三");//则修改该属性为“update张三”
157
158
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
159
foreach(XmlNode xn1 in nls)//遍历
160

{
161
XmlElement xe2=(XmlElement)xn1;//转换类型
162
if(xe2.Name=="author")//如果找到
163

{
164
xe2.InnerText="亚胜";//则修改
165
}
166
}
167
}
168
}
169
xmlDoc.Save( Server.MapPath("data.xml") );//保存。
170
171
/**///////////////////////////////////////////////////////////////////////////////////////
172
结果:将原来的所有结点的信息都修改了,xml的内容如下,
173
<?xml version="1.0"?>
174
<Employees>
175
<Node genre="李赞红" ISBN="2-3631-4">
176
<title>CS从入门到精通</title>
177
<author>候捷</author>
178
<price>58.3</price>
179
</Node>
180
<Node genre="李赞红" ISBN="2-3631-4">
181
<title>CS从入门到精通</title>
182
<author>候捷</author>
183
<price>58.3</price>
184
</Node>
185
<Node genre="update张三" ISBN="1-1111-1">
186
<title>C#入门帮助</title>
187
<author>亚胜</author>
188
<price>158.3</price>
189
</Node>
190
</Employees>
191
192
4,修改结点(添加结点的属性和添加结点的自结点):
193
XmlDocument xmlDoc=new XmlDocument();
194
xmlDoc.Load( Server.MapPath("data.xml") );
195
196
XmlNodeList nodeList=xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点
197
198
foreach(XmlNode xn in nodeList)
199

{
200
XmlElement xe=(XmlElement)xn;
201
xe.SetAttribute("test","111111");
202
203
XmlElement xesub=xmlDoc.CreateElement("flag");
204
xesub.InnerText="1";
205
xe.AppendChild(xesub);
206
}
207
xmlDoc.Save( Server.MapPath("data.xml") );
208
209
/**///////////////////////////////////////////////////////////////////////////////////////
210
结果:每个结点的属性都添加了一个,子结点也添加了一个,内容如下,
211
<?xml version="1.0"?>
212
<Employees>
213
<Node genre="李赞红" ISBN="2-3631-4" test="111111">
214
<title>CS从入门到精通</title>
215
<author>候捷</author>
216
<price>58.3</price>
217
<flag>1</flag>
218
</Node>
219
<Node genre="李赞红" ISBN="2-3631-4" test="111111">
220
<title>CS从入门到精通</title>
221
<author>候捷</author>
222
<price>58.3</price>
223
<flag>1</flag>
224
</Node>
225
<Node genre="update张三" ISBN="1-1111-1" test="111111">
226
<title>C#入门帮助</title>
227
<author>亚胜</author>
228
<price>158.3</price>
229
<flag>1</flag>
230
</Node>
231
</Employees>
232
233
5,删除结点中的某一个属性:
234
XmlDocument xmlDoc=new XmlDocument();
235
xmlDoc.Load( Server.MapPath("data.xml") );
236
XmlNodeList xnl=xmlDoc.SelectSingleNode("Employees").ChildNodes;
237
foreach(XmlNode xn in xnl)
238

{
239
XmlElement xe=(XmlElement)xn;
240
xe.RemoveAttribute("genre");//删除genre属性
241
242
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
243
foreach(XmlNode xn1 in nls)//遍历
244

{
245
XmlElement xe2=(XmlElement)xn1;//转换类型
246
if(xe2.Name=="flag")//如果找到
247

{
248
xe.RemoveChild(xe2);//则删除
249
}
250
}
251
}
252
xmlDoc.Save( Server.MapPath("data.xml") );
253
254
/**///////////////////////////////////////////////////////////////////////////////////////]
255
结果:删除了结点的一个属性和结点的一个子结点,内容如下,
256
<?xml version="1.0"?>
257
<Employees>
258
<Node ISBN="2-3631-4" test="111111">
259
<title>CS从入门到精通</title>
260
<author>候捷</author>
261
<price>58.3</price>
262
</Node>
263
<Node ISBN="2-3631-4" test="111111">
264
<title>CS从入门到精通</title>
265
<author>候捷</author>
266
<price>58.3</price>
267
</Node>
268
<Node ISBN="1-1111-1" test="111111">
269
<title>C#入门帮助</title>
270
<author>亚胜</author>
271
<price>158.3</price>
272
</Node>
273
</Employees>
274
275
6,删除结点:
276
XmlDocument xmlDoc=new XmlDocument();
277
xmlDoc.Load( Server.MapPath("data.xml") );
278
XmlNode root=xmlDoc.SelectSingleNode("Employees");
279
XmlNodeList xnl=xmlDoc.SelectSingleNode("Employees").ChildNodes;
280
for(int i=0;i<xnl.Count;i++)
281

{
282
XmlElement xe=(XmlElement)xnl.Item(i);
283
if(xe.GetAttribute("genre")=="张三")
284

{
285
root.RemoveChild(xe);
286
if(i<xnl.Count)i=i-1;
287
}
288
}
289
xmlDoc.Save( Server.MapPath("data.xml") );
290
291
/**///////////////////////////////////////////////////////////////////////////////////////]
292
结果:删除了符合条件的所有结点,原来的内容:
293
294
<?xml version="1.0"?>
295
<Employees>
296
<Node genre="李赞红" ISBN="2-3631-4">
297
<title>CS从入门到精通</title>
298
<author>候捷</author>
299
<price>58.3</price>
300
</Node>
301
<Node genre="李赞红" ISBN="2-3631-4">
302
<title>CS从入门到精通</title>
303
<author>候捷</author>
304
<price>58.3</price>
305
</Node>
306
<Node genre="张三" ISBN="1-1111-1">
307
<title>C#入门帮助</title>
308
<author>高手</author>
309
<price>158.3</price>
310
</Node>
311
<Node genre="张三" ISBN="1-1111-1">
312
<title>C#入门帮助</title>
313
<author>高手</author>
314
<price>158.3</price>
315
</Node>
316
</Employees>
317
318
删除后的内容:
319
<?xml version="1.0"?>
320
<Employees>
321
<Node genre="李赞红" ISBN="2-3631-4">
322
<title>CS从入门到精通</title>
323
<author>候捷</author>
324
<price>58.3</price>
325
</Node>
326
<Node genre="李赞红" ISBN="2-3631-4">
327
<title>CS从入门到精通</title>
328
<author>候捷</author>
329
<price>58.3</price>
330
</Node>
331
</Employees>
332

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23



24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45


46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90


91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123


124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152



153

154

155



156

157

158

159

160



161

162

163



164

165

166

167

168

169

170

171


172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199



200

201

202

203

204

205

206

207

208

209


210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238



239

240

241

242

243

244



245

246

247



248

249

250

251

252

253

254


255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281



282

283

284



285

286

287

288

289

290

291


292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332
