1 ///
2 /// XML文档操作工具类
3 ///
4 public class XmlUtil
5 {
6 #region XML文档节点查询和读取
7
8 ///
9 /// 选择匹配XPath表达式的第一个节点XmlNode.
10 ///
11 /// XML文档
12 /// XML文档完全文件名(包含物理路径)
13 /// 要匹配的XPath表达式(例如:"//节点名//子节点名")
14 /// 返回XmlNode
15 public static XmlNode GetXmlNodeByXpath(XmlDocument xmlDoc, string xpath)
16 {
17 try
18 {
19 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
20 return xmlNode;
21 }
22 catch (Exception ex)
23 {
24 throw ex; //这里可以定义你自己的异常处理
25 }
26 }
27
28 ///
29 /// 选择匹配XPath表达式的节点列表XmlNodeList.
30 ///
31 /// XML文档完全文件名(包含物理路径)
32 /// 要匹配的XPath表达式(例如:"//节点名//子节点名")
33 /// 返回XmlNodeList
34 public static XmlNodeList GetXmlNodeListByXpath(XmlDocument xmlDoc, string xpath)
35 {
36 try
37 {
38 XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xpath);
39 return xmlNodeList;
40 }
41 catch (Exception ex)
42 {
43 throw ex; //这里可以定义你自己的异常处理
44 }
45 }
46
47 ///
48 /// 选择匹配XPath表达式的第一个节点的匹配xmlAttributeName的属性XmlAttribute.
49 ///
50 /// XML文档完全文件名(包含物理路径)
51 /// 要匹配的XPath表达式(例如:"//节点名//子节点名
52 /// 要匹配xmlAttributeName的属性名称,默认为value
53 /// 返回xmlAttributeName
54 public static XmlAttribute GetXmlAttribute(XmlDocument xmlDoc, string xpath, params string[] xmlAttributeName)
55 {
56 string content = string.Empty;
57 XmlAttribute xmlAttribute = null;
58 try
59 {
60 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
61 if (xmlNode != null)
62 {
63 if (xmlNode.Attributes.Count > 0)
64 {
65 if (int.Equals(xmlAttributeName.Length, 0))
66 xmlAttribute = xmlNode.Attributes["value"];
67 else
68 xmlAttribute = xmlNode.Attributes[xmlAttributeName[0]];
69 }
70 }
71 }
72 catch (Exception ex)
73 {
74 throw ex; //这里可以定义你自己的异常处理
75 }
76 return xmlAttribute;
77 }
78
79 #endregion
80
81 #region XML文档创建和节点或属性的添加、修改
82
83 ///
84 /// 创建一个XML文档
85 ///
86 /// XML文档完全文件名(包含物理路径)
87 /// XML文档根节点名称(须指定一个根节点名称)
88 /// XML文档信息量(必须为:"1.0")
89 /// XML文档编码方式
90 /// 该值必须是"yes"或"no",如果为null,Save方法不在XML声明上写出独立属性
91 /// 成功返回true,失败返回false
92 public static bool CreateXmlDocument(string xmlFileName, string rootNodeName, string version, string encoding, string standalone)
93 {
94 bool isSuccess = false;
95 try
96 {
97 XmlDocument xmlDoc = new XmlDocument();
98 XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone);
99 XmlNode root = xmlDoc.CreateElement(rootNodeName);
100 xmlDoc.AppendChild(xmlDeclaration);
101 xmlDoc.AppendChild(root);
102 xmlDoc.Save(xmlFileName);
103 isSuccess = true;
104 }
105 catch (Exception ex)
106 {
107 throw ex; //这里可以定义你自己的异常处理
108 }
109 return isSuccess;
110 }
111
112 ///
113 /// 依据匹配XPath表达式的第一个节点来创建它的子节点(如果此节点已存在则追加一个新的同名节点
114 ///
115 /// XML文档完全文件名(包含物理路径)
116 /// 要匹配的XPath表达式(例如:"//节点名//子节点名
117 /// 要匹配xmlNodeName的节点名称
118 /// 节点文本值
119 /// 要匹配xmlAttributeName的属性名称集合
120 /// 属性值集合
121 /// 成功返回true,失败返回false
122 public static bool CreateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText, string[] xmlAttributeName, string[] value)
123 {
124 bool isSuccess = false;
125 XmlDocument xmlDoc = new XmlDocument();
126 try
127 {
128 xmlDoc.Load(xmlFileName); //加载XML文档
129 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
130 if (xmlNode != null)
131 {
132 //存不存在此节点都创建
133 XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
134 subElement.InnerXml = innerText;
135
136 //如果属性和值参数都不为空则在此新节点上新增属性
137 if (xmlAttributeName != null && value != null)
138 {
139 for (int i = 0; i < xmlAttributeName.Length; i++)
140 {
141 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName[i]);
142 xmlAttribute.Value = value[i];
143 subElement.Attributes.Append(xmlAttribute);
144 }
145 }
146 xmlNode.AppendChild(subElement);
147 }
148 xmlDoc.Save(xmlFileName); //保存到XML文档
149 isSuccess = true;
150 }
151 catch (Exception ex)
152 {
153 throw ex; //这里可以定义你自己的异常处理
154 }
155 return isSuccess;
156 }
157
158 ///
159 /// 依据匹配XPath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建)
160 ///
161 /// XML文档完全文件名(包含物理路径)
162 /// 要匹配的XPath表达式(例如:"//节点名//子节点名
163 /// 要匹配xmlNodeName的节点名称
164 /// 节点文本值
165 /// 成功返回true,失败返回false
166 public static bool CreateOrUpdateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText)
167 {
168 bool isSuccess = false;
169 bool isExistsNode = false;//标识节点是否存在
170 XmlDocument xmlDoc = new XmlDocument();
171 try
172 {
173 xmlDoc.Load(xmlFileName); //加载XML文档
174 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
175 if (xmlNode != null)
176 {
177 //遍历xpath节点下的所有子节点
178 foreach (XmlNode node in xmlNode.ChildNodes)
179 {
180 if (node.Name.ToLower() == xmlNodeName.ToLower())
181 {
182 //存在此节点则更新
183 node.InnerXml = innerText;
184 isExistsNode = true;
185 break;
186 }
187 }
188 if (!isExistsNode)
189 {
190 //不存在此节点则创建
191 XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
192 subElement.InnerXml = innerText;
193 xmlNode.AppendChild(subElement);
194 }
195 }
196 xmlDoc.Save(xmlFileName); //保存到XML文档
197 isSuccess = true;
198 }
199 catch (Exception ex)
200 {
201 throw ex; //这里可以定义你自己的异常处理
202 }
203 return isSuccess;
204 }
205
206 ///
207 /// 依据匹配XPath表达式的第一个节点来创建或更新它的属性(如果属性存在则更新,不存在则创建)
208 ///
209 /// XML文档完全文件名(包含物理路径)
210 /// 要匹配的XPath表达式(例如:"//节点名//子节点名
211 /// 要匹配xmlAttributeName的属性名称
212 /// 属性值
213 /// 成功返回true,失败返回false
214 public static bool CreateOrUpdateXmlAttributeByXPath(string xmlFileName, string xpath, string value, params string[] xmlAttributeNamePara)
215 {
216 bool isSuccess = false;
217 bool isExistsAttribute = false;//标识属性是否存在
218 XmlDocument xmlDoc = new XmlDocument();
219 try
220 {
221 xmlDoc.Load(xmlFileName); //加载XML文档
222 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
223 if (xmlNode != null)
224 {
225 //默认为value
226 string xmlAttributeName = "value";
227 if (xmlAttributeNamePara.Length > 0)
228 {
229 xmlAttributeName = xmlAttributeNamePara[0];
230 }
231 //遍历xpath节点中的所有属性
232 foreach (XmlAttribute attribute in xmlNode.Attributes)
233 {
234 if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
235 {
236 //节点中存在此属性则更新
237 attribute.Value = value;
238 isExistsAttribute = true;
239 break;
240 }
241 }
242 if (!isExistsAttribute)
243 {
244 //节点中不存在此属性则创建
245 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName);
246 xmlAttribute.Value = value;
247 xmlNode.Attributes.Append(xmlAttribute);
248 }
249 }
250 xmlDoc.Save(xmlFileName); //保存到XML文档
251 isSuccess = true;
252 }
253 catch (Exception ex)
254 {
255 throw ex; //这里可以定义你自己的异常处理
256 }
257 return isSuccess;
258 }
259
260 #endregion
261
262 #region XML文档节点或属性的删除
263
264 ///
265 /// 删除匹配XPath表达式的第一个节点(节点中的子元素同时会被删除)
266 ///
267 /// XML文档完全文件名(包含物理路径)
268 /// 要匹配的XPath表达式(例如:"//节点名//子节点名
269 /// 成功返回true,失败返回false
270 public static bool DeleteXmlNodeByXPath(string xmlFileName, string xpath)
271 {
272 bool isSuccess = false;
273 XmlDocument xmlDoc = new XmlDocument();
274 try
275 {
276 xmlDoc.Load(xmlFileName); //加载XML文档
277 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
278 if (xmlNode != null)
279 {
280 //删除节点
281 xmlNode.ParentNode.RemoveChild(xmlNode);
282 }
283 xmlDoc.Save(xmlFileName); //保存到XML文档
284 isSuccess = true;
285 }
286 catch (Exception ex)
287 {
288 throw ex; //这里可以定义你自己的异常处理
289 }
290 return isSuccess;
291 }
292
293 ///
294 /// 删除匹配XPath表达式的第一个节点中的匹配参数xmlAttributeName的属性
295 ///
296 /// XML文档完全文件名(包含物理路径)
297 /// 要匹配的XPath表达式(例如:"//节点名//子节点名
298 /// 要删除的xmlAttributeName的属性名称
299 /// 成功返回true,失败返回false
300 public static bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName)
301 {
302 bool isSuccess = false;
303 bool isExistsAttribute = false;
304 XmlDocument xmlDoc = new XmlDocument();
305 try
306 {
307 xmlDoc.Load(xmlFileName); //加载XML文档
308 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
309 XmlAttribute xmlAttribute = null;
310 if (xmlNode != null)
311 {
312 //遍历xpath节点中的所有属性
313 foreach (XmlAttribute attribute in xmlNode.Attributes)
314 {
315 if (attribute.Name.ToLower() == xmlAttributeName.ToLower())
316 {
317 //节点中存在此属性
318 xmlAttribute = attribute;
319 isExistsAttribute = true;
320 break;
321 }
322 }
323 if (isExistsAttribute)
324 {
325 //删除节点中的属性
326 xmlNode.Attributes.Remove(xmlAttribute);
327 }
328 }
329 xmlDoc.Save(xmlFileName); //保存到XML文档
330 isSuccess = true;
331 }
332 catch (Exception ex)
333 {
334 throw ex; //这里可以定义你自己的异常处理
335 }
336 return isSuccess;
337 }
338
339 ///
340 /// 删除匹配XPath表达式的第一个节点中的所有属性
341 ///
342 /// XML文档完全文件名(包含物理路径)
343 /// 要匹配的XPath表达式(例如:"//节点名//子节点名
344 /// 成功返回true,失败返回false
345 public static bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath)
346 {
347 bool isSuccess = false;
348 XmlDocument xmlDoc = new XmlDocument();
349 try
350 {
351 xmlDoc.Load(xmlFileName); //加载XML文档
352 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);
353 if (xmlNode != null)
354 {
355 //遍历xpath节点中的所有属性
356 xmlNode.Attributes.RemoveAll();
357 }
358 xmlDoc.Save(xmlFileName); //保存到XML文档
359 isSuccess = true;
360 }
361 catch (Exception ex)
362 {
363 throw ex; //这里可以定义你自己的异常处理
364 }
365 return isSuccess;
366 }
367
368 #endregion
369 }