经过这些日子以来,我根据上篇随笔中读写程序配置文件节点的方法来扩展了其功能,并写了这个XML文档的操作管理帮助类XMLHelper出来,这个XMLHelper类中包括了XML文档的创建,文档节点和属性的读取,添加,修改,删除的方法功能的实现,有兴趣的朋友,可以进来看看,所有代码都在WebForm和WinForm中调试通过.
参考我的上篇随笔:关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
这是下面要操作的XML文档:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
XML文档
<?xml version="1.0" encoding="utf-8"?>
<books>
<book id="1" ISDN="1001001001">
<name>我的世界我的梦</name>
<author>姚明</author>
<date>2008-09-23</date>
</book>
<book id="2" ISDN="2002000230032">
<name>围城</name>
<author>钱钟书</author>
<date>2008-09-23</date>
</book>
<book id="3" />
</books>
以下是XMLHelper文档操作帮助类代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
XMLHelper.cs XML文档操作帮助类 Code
1
//======================================================================
2
//
3
// Copyright(C) 2009-2010 连林SoftWare工作室
4
// All Rights Reserved
5
//
6
// FileName: XMLHelper
7
// Description: XML文档帮助类,静态方法,实现对
8
// XML文档的创建,及节点和属性的增、删、改、查
9
//
10
// Author: Wang Lian Lin(王连林)
11
// CLR版本: 2.0.50727.42
12
// MachineName: WLL
13
// 注册组织名: WLL
14
// Created By Wang Lian Lin(王连林) at 2009-3-22 16:50:44
15
// Email: LianLin.Wang@163.com
16
// http://chnboy.cnblogs.com
17
//
18
//======================================================================
19
using System;
20
using System.Xml;
21![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
23
/// XMLHelper XML文档操作管理器
24
/// </summary>
25
public class XMLHelper
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
27
public XMLHelper()
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
//
30
// TODO: 在此处添加构造函数逻辑
31
//
32
}
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
XML文档节点查询和读取#region XML文档节点查询和读取
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
37
/// 选择匹配XPath表达式的第一个节点XmlNode.
38
/// </summary>
39
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
40
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>
41
/// <returns>返回XmlNode</returns>
42
public static XmlNode GetXmlNodeByXpath(string xmlFileName, string xpath)
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
XmlDocument xmlDoc = new XmlDocument();
45
try
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
xmlDoc.Load(xmlFileName); //加载XML文档
48
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
49
return xmlNode;
50
}
51
catch (Exception ex)
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
return null;
54
//throw ex; //这里可以定义你自己的异常处理
55
}
56
}
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
59
/// 选择匹配XPath表达式的节点列表XmlNodeList.
60
/// </summary>
61
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
62
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>
63
/// <returns>返回XmlNodeList</returns>
64
public static XmlNodeList GetXmlNodeListByXpath(string xmlFileName, string xpath)
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
XmlDocument xmlDoc = new XmlDocument();
67![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
try
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
xmlDoc.Load(xmlFileName); //加载XML文档
71
XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xpath);
72
return xmlNodeList;
73
}
74
catch (Exception ex)
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
return null;
77
//throw ex; //这里可以定义你自己的异常处理
78
}
79
}
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
82
/// 选择匹配XPath表达式的第一个节点的匹配xmlAttributeName的属性XmlAttribute.
83
/// </summary>
84
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
85
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
86
/// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
87
/// <returns>返回xmlAttributeName</returns>
88
public static XmlAttribute GetXmlAttribute(string xmlFileName, string xpath, string xmlAttributeName)
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
string content = string.Empty;
91
XmlDocument xmlDoc = new XmlDocument();
92
XmlAttribute xmlAttribute = null;
93
try
94![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
95
xmlDoc.Load(xmlFileName); //加载XML文档
96
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
97
if (xmlNode != null)
98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
99
if (xmlNode.Attributes.Count > 0)
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
xmlAttribute = xmlNode.Attributes[xmlAttributeName];
102
}
103
}
104
}
105
catch (Exception ex)
106![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
107
throw ex; //这里可以定义你自己的异常处理
108
}
109
return xmlAttribute;
110
}
111
#endregion
112![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
XML文档创建和节点或属性的添加、修改#region XML文档创建和节点或属性的添加、修改
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
115
/// 创建一个XML文档
116
/// </summary>
117
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
118
/// <param name="rootNodeName">XML文档根节点名称(须指定一个根节点名称)</param>
119
/// <param name="version">XML文档版本号(必须为:"1.0")</param>
120
/// <param name="encoding">XML文档编码方式</param>
121
/// <param name="standalone">该值必须是"yes"或"no",如果为null,Save方法不在XML声明上写出独立属性</param>
122
/// <returns>成功返回true,失败返回false</returns>
123
public static bool CreateXmlDocument(string xmlFileName, string rootNodeName, string version, string encoding, string standalone)
124![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
125
bool isSuccess = false;
126
try
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
XmlDocument xmlDoc = new XmlDocument();
129
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone);
130
XmlNode root = xmlDoc.CreateElement(rootNodeName);
131
xmlDoc.AppendChild(xmlDeclaration);
132
xmlDoc.AppendChild(root);
133
xmlDoc.Save(xmlFileName);
134
isSuccess = true;
135
}
136
catch (Exception ex)
137![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
138
throw ex; //这里可以定义你自己的异常处理
139
}
140
return isSuccess;
141
}
142![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
144
/// 依据匹配XPath表达式的第一个节点来创建它的子节点(如果此节点已存在则追加一个新的同名节点
145
/// </summary>
146
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
147
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
148
/// <param name="xmlNodeName">要匹配xmlNodeName的节点名称</param>
149
/// <param name="innerText">节点文本值</param>
150
/// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
151
/// <param name="value">属性值</param>
152
/// <returns>成功返回true,失败返回false</returns>
153
public static bool CreateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText, string xmlAttributeName, string value)
154![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
155
bool isSuccess = false;
156
XmlDocument xmlDoc = new XmlDocument();
157
try
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
159
xmlDoc.Load(xmlFileName); //加载XML文档
160
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
161
if (xmlNode != null)
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
163
//存不存在此节点都创建
164
XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
165
subElement.InnerXml = innerText;
166![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
167
//如果属性和值参数都不为空则在此新节点上新增属性
168
if (!string.IsNullOrEmpty(xmlAttributeName) && !string.IsNullOrEmpty(value))
169![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
170
XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName);
171
xmlAttribute.Value = value;
172
subElement.Attributes.Append(xmlAttribute);
173
}
174![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
175
xmlNode.AppendChild(subElement);
176
}
177
xmlDoc.Save(xmlFileName); //保存到XML文档
178
isSuccess = true;
179
}
180
catch (Exception ex)
181![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
182
throw ex; //这里可以定义你自己的异常处理
183
}
184
return isSuccess;
185
}
186![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
187![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
188
/// 依据匹配XPath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建)
189
/// </summary>
190
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
191
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
192
/// <param name="xmlNodeName">要匹配xmlNodeName的节点名称</param>
193
/// <param name="innerText">节点文本值</param>
194
/// <returns>成功返回true,失败返回false</returns>
195
public static bool CreateOrUpdateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText)
196![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
197
bool isSuccess = false;
198
bool isExistsNode = false;//标识节点是否存在
199
XmlDocument xmlDoc = new XmlDocument();
200
try
201![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
202
xmlDoc.Load(xmlFileName); //加载XML文档
203
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
204
if (xmlNode != null)
205![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
206
//遍历xpath节点下的所有子节点
207
foreach (XmlNode node in xmlNode.ChildNodes)
208![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
209
if (node.Name.ToLower() == xmlNodeName.ToLower())
210![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
211
//存在此节点则更新
212
node.InnerXml = innerText;
213
isExistsNode = true;
214
break;
215
}
216
}
217
if (!isExistsNode)
218![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
219
//不存在此节点则创建
220
XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
221
subElement.InnerXml = innerText;
222
xmlNode.AppendChild(subElement);
223
}
224
}
225
xmlDoc.Save(xmlFileName); //保存到XML文档
226
isSuccess = true;
227
}
228
catch (Exception ex)
229![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
230
throw ex; //这里可以定义你自己的异常处理
231
}
232
return isSuccess;
233
}
234![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
235![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
236
/// 依据匹配XPath表达式的第一个节点来创建或更新它的属性(如果属性存在则更新,不存在则创建)
237
/// </summary>
238
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
239
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
240
/// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>
241
/// <param name="value">属性值</param>
242
/// <returns>成功返回true,失败返回false</returns>
243
public static bool CreateOrUpdateXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName, string value)
244![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
245
bool isSuccess = false;
246
bool isExistsAttribute = false;//标识属性是否存在
247
XmlDocument xmlDoc = new XmlDocument();
248
try
249![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
250
xmlDoc.Load(xmlFileName); //加载XML文档
251
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
252
if (xmlNode != null)
253![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
254
//遍历xpath节点中的所有属性
255
foreach (XmlAttribute attribute in xmlNode.Attributes)
256![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
257
if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
258![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
259
//节点中存在此属性则更新
260
attribute.Value = value;
261
isExistsAttribute = true;
262
break;
263
}
264
}
265
if (!isExistsAttribute)
266![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
267
//节点中不存在此属性则创建
268
XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName);
269
xmlAttribute.Value = value;
270
xmlNode.Attributes.Append(xmlAttribute);
271
}
272
}
273
xmlDoc.Save(xmlFileName); //保存到XML文档
274
isSuccess = true;
275
}
276
catch (Exception ex)
277![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
278
throw ex; //这里可以定义你自己的异常处理
279
}
280
return isSuccess;
281
}
282
#endregion
283![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
284![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
XML文档节点或属性的删除#region XML文档节点或属性的删除
285![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
286
/// 删除匹配XPath表达式的第一个节点(节点中的子元素同时会被删除)
287
/// </summary>
288
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
289
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
290
/// <returns>成功返回true,失败返回false</returns>
291
public static bool DeleteXmlNodeByXPath(string xmlFileName, string xpath)
292![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
293
bool isSuccess = false;
294
XmlDocument xmlDoc = new XmlDocument();
295
try
296![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
297
xmlDoc.Load(xmlFileName); //加载XML文档
298
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
299
if (xmlNode != null)
300![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
301
//删除节点
302
xmlNode.ParentNode.RemoveChild(xmlNode);
303
}
304
xmlDoc.Save(xmlFileName); //保存到XML文档
305
isSuccess = true;
306
}
307
catch (Exception ex)
308![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
309
throw ex; //这里可以定义你自己的异常处理
310
}
311
return isSuccess;
312
}
313![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
314![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
315
/// 删除匹配XPath表达式的第一个节点中的匹配参数xmlAttributeName的属性
316
/// </summary>
317
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
318
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
319
/// <param name="xmlAttributeName">要删除的xmlAttributeName的属性名称</param>
320
/// <returns>成功返回true,失败返回false</returns>
321
public static bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName)
322![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
323
bool isSuccess = false;
324
bool isExistsAttribute = false;
325
XmlDocument xmlDoc = new XmlDocument();
326
try
327![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
328
xmlDoc.Load(xmlFileName); //加载XML文档
329
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
330
XmlAttribute xmlAttribute = null;
331
if (xmlNode != null)
332![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
333
//遍历xpath节点中的所有属性
334
foreach (XmlAttribute attribute in xmlNode.Attributes)
335![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
336
if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
337![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
338
//节点中存在此属性
339
xmlAttribute = attribute;
340
isExistsAttribute = true;
341
break;
342
}
343
}
344
if (isExistsAttribute)
345![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
346
//删除节点中的属性
347
xmlNode.Attributes.Remove(xmlAttribute);
348
}
349
}
350
xmlDoc.Save(xmlFileName); //保存到XML文档
351
isSuccess = true;
352
}
353
catch (Exception ex)
354![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
355
throw ex; //这里可以定义你自己的异常处理
356
}
357
return isSuccess;
358
}
359![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
360![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
361
/// 删除匹配XPath表达式的第一个节点中的所有属性
362
/// </summary>
363
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
364
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
365
/// <returns>成功返回true,失败返回false</returns>
366
public static bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath)
367![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
368
bool isSuccess = false;
369
XmlDocument xmlDoc = new XmlDocument();
370
try
371![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
372
xmlDoc.Load(xmlFileName); //加载XML文档
373
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
374
if (xmlNode != null)
375![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
376
//遍历xpath节点中的所有属性
377
xmlNode.Attributes.RemoveAll();
378
}
379
xmlDoc.Save(xmlFileName); //保存到XML文档
380
isSuccess = true;
381
}
382
catch (Exception ex)
383![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
384
throw ex; //这里可以定义你自己的异常处理
385
}
386
return isSuccess;
387
}
388
#endregion
389![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
390
}
391![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1.创建XML文档:
//这是XML文档根节点名
string rootNodeName = "books";
//这是XML文档物理文件名(包含物理路径)
string xmlFileName = Application.StartupPath + @"\book.xml";
XMLHelper.CreateXmlDocument(xmlFileName, rootNodeName, "1.0", "utf-8", null);
MessageBox.Show("XML文档创建成功:" + xmlFileName);
2.向XML文档中添加一个新节点:
string xmlFileName = Application.StartupPath + @"\book.xml";
string xpath = "/books"; //这是新节点的父节点路径
string nodename = "book"; //这是新节点名称,在父节点下新增
string nodetext = "这是新节点中的文本值";
bool isSuccess = XMLHelper.CreateOrUpdateXmlNodeByXPath(xmlFileName, xpath, nodename, nodetext);
MessageBox.Show("XML节点添加或更新成功:" + isSuccess.ToString());
3.向XML文档中的子节点中新增或修改(如果存在则修改)一个子节点,比如name,author,date节点等:
string xmlFileName = Application.StartupPath + @"\book.xml";
string xpath = "/books/book"; //这是新子节点的父节点路径
string nodename = "name"; //这是新子节点名称,在父节点下新增
string nodetext = "我的世界我的梦";
bool isSuccess = XMLHelper.CreateOrUpdateXmlNodeByXPath(xmlFileName, xpath, nodename, nodetext);
MessageBox.Show("XML节点添加或更新成功:" + isSuccess.ToString());
4. 向XML文档中的子节点中新增或修改(如果存在则修改)一个子节点属性,比如id,ISDN属性等:
string xmlFileName = Application.StartupPath + @"\book.xml";
string xpath = "/books/book"; //要新增属性的节点
string attributeName = "id"; //新属性名称,ISDN号也是这么新增的
string attributeValue = "1"; //新属性值
bool isSuccess = XMLHelper.CreateOrUpdateXmlAttributeByXPath(xmlFileName, xpath, attributeName, attributeValue);
MessageBox.Show("XML属性添加或更新成功:" + isSuccess.ToString());
5. 删除XML文档中的子节点:
string xmlFileName = Application.StartupPath + @"\book.xml";
string xpath = "/books/book[@id='1']"; //要删除的id为1的book子节点
bool isSuccess = XMLHelper.DeleteXmlNodeByXPath(xmlFileName, xpath);
MessageBox.Show("XML节点删除成功:" + isSuccess.ToString());
6. 删除XML文档中子节点的属性:
string xmlFileName = Application.StartupPath + @"\book.xml";
//删除id为2的book子节点中的ISDN属性
string xpath = "/books/book[@id='2']";
string attributeName = "ISDN";
bool isSuccess = XMLHelper.DeleteXmlAttributeByXPath(xmlFileName, xpath,attributeName);
MessageBox.Show("XML属性删除成功:" + isSuccess.ToString());
7.读取XML文档中的所有子节点:
string xmlFileName = Application.StartupPath + @"\book.xml";
//要读的id为1的book子节点
string xpath = "/books/book[@id='1']";
XmlNodeList nodeList = XMLHelper.GetXmlNodeListByXpath(xmlFileName, xpath);
string strAllNode = "";
//遍历节点中所有的子节点
foreach (XmlNode node in nodeList)
{
strAllNode += "\n name:" + node.Name + " InnerText:" + node.InnerText;
}
MessageBox.Show("XML节点中所有子节点有:" + strAllNode);
8.其它的方法我就不一一的例举了,各位自己动手去尝试便知,关键的地方就是那个xpath的参数设置了,
这个是XML文档中xpath语法,大家去网上一查便明白,好了,我要休息去,明白偶还有很多的工作做...