xml文件net操纵类(c#) [转载]
1using System;
2using System.Xml;
3using System.Web;
4namespace MX.XML
5{
6 /// <summary>
7 /// XML核心类:
8 /// 必需用XPath表达式来获取相应节点
9 /// </summary>
10 public class Core
11 {
12 #region 变量-----------------------------------------------------------
13 /// <summary>
14 /// xml文件所在路径类型
15 /// </summary>
16 /// <remarks>xml文件所在路径类型</remarks>
17 public enum enumXmlPathType
18 {
19 /// <summary>
20 /// 绝对路径
21 /// </summary>
22 AbsolutePath,
23 /// <summary>
24 /// 虚拟路径
25 /// </summary>
26 VirtualPath
27 }
28
29 private string xmlFilePath;
30 private enumXmlPathType xmlFilePathType;
31 private XmlDocument xmlDoc = new XmlDocument();
32 #endregion
33
34 #region 属性-----------------------------------------------------------
35 /// <summary>
36 /// 文件路径
37 /// </summary>
38 /// <remarks>文件路径</remarks>
39 public string XmlFilePath
40 {
41 get
42 {
43 return this.xmlFilePath;
44 }
45 set
46 {
47 xmlFilePath = value;
48
49 }
50 }
51
52 /// <summary>
53 /// 文件路径类型
54 /// </summary>
55 public enumXmlPathType XmlFilePathTyp
56 {
57 set
58 {
59 xmlFilePathType = value;
60 }
61 }
62 #endregion
63
64 #region 构造函数-------------------------------------------------------
65 /// <summary>
66 /// 构造函数
67 /// </summary>
68 /// <param >文件路径</param>
69 public Core(string tempXmlFilePath)
70 {
71 this.xmlFilePathType = enumXmlPathType.VirtualPath;
72 this.xmlFilePath = tempXmlFilePath;
73 GetXmlDocument();
74 //xmlDoc.Load( xmlFilePath ) ;
75 }
76
77 /// <summary>
78 /// 构造函数
79 /// </summary>
80 /// <param >文件路径</param>
81 /// <param >类型</param>
82 public Core(string tempXmlFilePath, enumXmlPathType tempXmlFilePathType)
83 {
84 this.xmlFilePathType = tempXmlFilePathType;
85 this.xmlFilePath = tempXmlFilePath;
86 GetXmlDocument();
87 }
88
89 ///<summary>
90 ///获取XmlDocument实体类
91 ///</summary>
92 /// <returns>指定的XML描述文件的一个xmldocument实例</returns>
93 private XmlDocument GetXmlDocument()
94 {
95 XmlDocument doc = null;
96
97 if (this.xmlFilePathType == enumXmlPathType.AbsolutePath)
98 {
99 doc = GetXmlDocumentFromFile(xmlFilePath);
100 }
101 else if (this.xmlFilePathType == enumXmlPathType.VirtualPath)
102 {
103 doc = GetXmlDocumentFromFile(HttpContext.Current.Server.MapPath(xmlFilePath));
104 }
105 return doc;
106 }
107
108 private XmlDocument GetXmlDocumentFromFile(string tempXmlFilePath)
109 {
110 string xmlFileFullPath = tempXmlFilePath;
111 xmlDoc.Load(xmlFileFullPath);
112 //定义事件处理
113 xmlDoc.NodeChanged += new XmlNodeChangedEventHandler(this.nodeUpdateEvent);
114 xmlDoc.NodeInserted += new XmlNodeChangedEventHandler(this.nodeInsertEvent);
115 xmlDoc.NodeRemoved += new XmlNodeChangedEventHandler(this.nodeDeleteEvent);
116 return xmlDoc;
117 }
118
119 #endregion
120
121 #region 获取所有指定名称的节点
122 /// <summary>
123 /// 功能:
124 /// 获取所有指定名称的节点(XmlNodeList)
125 /// </summary>
126 /// <param >节点名称</param>
127 public XmlNodeList GetXmlNodeList(string strNode)
128 {
129 XmlNodeList strReturn = null;
130 try
131 {
132 //根据指定路径获取节点
133 XmlNodeList xmlNode = xmlDoc.SelectNodes(strNode);
134 if (!(xmlNode == null))
135 {
136 strReturn = xmlNode;
137 }
138 }
139 catch (XmlException xmle)
140 {
141 throw xmle;
142 }
143 return strReturn;
144 }
145 #endregion
146
147 #region 读取指定节点的指定属性值---------------------------------------
148 /// <summary>
149 /// 功能:
150 /// 读取指定节点的指定属性值(Value)
151 /// </summary>
152 /// <param >节点名称</param>
153 /// <param >此节点的属性</param>
154 /// <returns></returns>
155 public string GetXmlNodeAttributeValue(string strNode, string strAttribute)
156 {
157 string strReturn = "";
158 try
159 {
160 //根据指定路径获取节点
161 XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
162 if (!(xmlNode == null))
163 {
164 strReturn = xmlNode.Attributes.GetNamedItem(strAttribute).Value;
165
166 ////获取节点的属性,并循环取出需要的属性值
167 //XmlAttributeCollection xmlAttr = xmlNode.Attributes;
168 //for (int i = 0; i < xmlAttr.Count; i++)
169 //{
170 // if (xmlAttr.Item(i).Name == strAttribute)
171 // {
172 // strReturn = xmlAttr.Item(i).Value;
173 // break;
174 // }
175 //}
176 }
177 }
178 catch (XmlException xmle)
179 {
180 throw xmle;
181 }
182 return strReturn;
183 }
184 #endregion
185
186 #region 读取指定节点的值-----------------------------------------------
187 /// <summary>
188 /// 功能:
189 /// 读取指定节点的值(InnerText)
190 /// </summary>
191 /// <param >节点名称</param>
192 /// <returns></returns>
193 public string GetXmlNodeValue(string strNode)
194 {
195 string strReturn = String.Empty;
196 try
197 {
198 //根据路径获取节点
199 XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
200 if (!(xmlNode == null))
201 strReturn = xmlNode.InnerText;
202 }
203 catch (XmlException xmle)
204 {
205 throw xmle;
206 }
207 return strReturn;
208 }
209 #endregion
210
211 #region 设置节点值-----------------------------------------------------
212 /// <summary>
213 /// 功能:
214 /// 设置节点值(InnerText)
215 /// </summary>
216 /// <param >节点的名称</param>
217 /// <param >节点值</param>
218 public void SetXmlNodeValue(string xmlNodePath, string xmlNodeValue)
219 {
220 try
221 {
222 //可以批量为符合条件的节点进行付值
223 XmlNodeList xmlNode = this.xmlDoc.SelectNodes(xmlNodePath);
224 if (!(xmlNode == null))
225 {
226 foreach (XmlNode xn in xmlNode)
227 {
228 xn.InnerText = xmlNodeValue;
229 }
230 }
231 /**/
232 /*
233 * 根据指定路径获取节点
234 XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;
235 //设置节点值
236 if (!(xmlNode==null))
237 xmlNode.InnerText = xmlNodeValue ;*/
238 }
239 catch (XmlException xmle)
240 {
241 throw xmle;
242 }
243 }
244 #endregion
245
246 #region 设置节点的属性值-----------------------------------------------
247 /// <summary>
248 /// 功能:
249 /// 设置节点的属性值
250 /// </summary>
251 /// <param >节点名称</param>
252 /// <param >属性名称</param>
253 /// <param >属性值</param>
254 public void SetXmlNodeAttributeValue(string xmlNodePath, string xmlNodeAttribute, string xmlNodeAttributeValue)
255 {
256 try
257 {
258 //可以批量为符合条件的节点的属性付值
259 XmlNodeList xmlNode = this.xmlDoc.SelectNodes(xmlNodePath);
260 if (!(xmlNode == null))
261 {
262 foreach (XmlNode xn in xmlNode)
263 {
264 XmlAttributeCollection xmlAttr = xn.Attributes;
265 for (int i = 0; i < xmlAttr.Count; i++)
266 {
267 if (xmlAttr.Item(i).Name == xmlNodeAttribute)
268 {
269 xmlAttr.Item(i).Value = xmlNodeAttributeValue;
270 break;
271 }
272 }
273 }
274 }
275 }
276 catch (XmlException xmle)
277 {
278 throw xmle;
279 }
280 }
281 #endregion
282
283 #region 添加-----------------------------------------------------------
284 /// <summary>
285 /// 获取XML文件的根元素
286 /// </summary>
287 public XmlNode GetXmlRoot()
288 {
289 return xmlDoc.DocumentElement;
290 }
291
292 /// <summary>
293 /// 在根节点下添加父节点
294 /// </summary>
295 public void AddParentNode(string parentNode)
296 {
297 try
298 {
299 XmlNode root = GetXmlRoot();
300 XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode);
301 root.AppendChild(parentXmlNode);
302 }
303 catch (XmlException xmle)
304 {
305 throw xmle;
306 }
307 }
308
309 /// <summary>
310 /// 向一个已经存在的父节点中插入一个子节点,并返回子节点.
311 /// </summary>
312 /// <param >父节点</param>
313 /// <param >字节点名称</param>
314 public XmlNode AddChildNode(string parentNodePath, string childnodename)
315 {
316 XmlNode childXmlNode = null;
317 try
318 {
319 XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);
320 if (!((parentXmlNode) == null))//如果此节点存在
321 {
322 childXmlNode = xmlDoc.CreateElement(childnodename);
323 parentXmlNode.AppendChild(childXmlNode);
324 }
325 else
326 {//如果不存在就放父节点添加
327 this.GetXmlRoot().AppendChild(childXmlNode);
328 }
329 }
330 catch (XmlException xmle)
331 {
332 throw xmle;
333 }
334 return childXmlNode;
335 }
336 /// <summary>
337 /// 向一个已经存在的父节点中插入一个子节点,并添加一个属性
338 /// </summary>
339 public void AddChildNode(string parentNodePath, string childnodename, string NodeAttribute, string NodeAttributeValue)
340 {
341 try
342 {
343 XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);
344 XmlNode childXmlNode = null;
345 if (!((parentXmlNode) == null))//如果此节点存在
346 {
347 childXmlNode = xmlDoc.CreateElement(childnodename);
348
349 //添加属性
350 XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);
351 nodeAttribute.Value = NodeAttributeValue;
352 childXmlNode.Attributes.Append(nodeAttribute);
353
354 parentXmlNode.AppendChild(childXmlNode);
355 }
356 else
357 {//如果不存在就放父节点添加
358 this.GetXmlRoot().AppendChild(childXmlNode);
359 }
360 }
361 catch (XmlException xmle)
362 {
363 throw xmle;
364 }
365 }
366
367 /// <summary>
368 /// 向一个节点添加属性,值为空
369 /// </summary>
370 /// <param >节点路径</param>
371 /// <param >属性名</param>
372 public void AddAttribute(string NodePath, string NodeAttribute)
373 {
374 privateAddAttribute(NodePath, NodeAttribute, "");
375 }
376 /// <summary>
377 /// 向一个节点添加属性,并赋值***
378 /// </summary>
379 public void AddAttribute(XmlNode childXmlNode, string NodeAttribute, string NodeAttributeValue)
380 {
381 XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);
382 nodeAttribute.Value = NodeAttributeValue;
383 childXmlNode.Attributes.Append(nodeAttribute);
384 }
385
386 /// <summary>
387 /// 向一个节点添加属性
388 /// </summary>
389 /// <param >节点路径</param>
390 /// <param >属性名</param>
391 /// <param >属性值</param>
392 private void privateAddAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)
393 {
394 try
395 {
396 XmlNode nodePath = xmlDoc.SelectSingleNode(NodePath);
397 if (!(nodePath == null))
398 {
399 XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);
400 nodeAttribute.Value = NodeAttributeValue;
401 nodePath.Attributes.Append(nodeAttribute);
402 }
403 }
404 catch (XmlException xmle)
405 {
406 throw xmle;
407 }
408 }
409 /// <summary>
410 /// 向一个节点添加属性,并赋值
411 /// </summary>
412 /// <param >节点</param>
413 /// <param >属性名</param>
414 /// <param >属性值</param>
415 public void AddAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)
416 {
417 privateAddAttribute(NodePath, NodeAttribute, NodeAttributeValue);
418 }
419 #endregion
420
421 #region 删除-----------------------------------------------------------
422 /// <summary>
423 /// 删除节点的一个属性
424 /// </summary>
425 /// <param >节点所在的xpath表达式</param>
426 /// <param >属性名</param>
427 public void DeleteAttribute(string NodePath, string NodeAttribute)
428 {
429 XmlNodeList nodePath = this.xmlDoc.SelectNodes(NodePath);
430 if (!(nodePath == null))
431 {
432 foreach (XmlNode tempxn in nodePath)
433 {
434 XmlAttributeCollection xmlAttr = tempxn.Attributes;
435 for (int i = 0; i < xmlAttr.Count; i++)
436 {
437 if (xmlAttr.Item(i).Name == NodeAttribute)
438 {
439 tempxn.Attributes.RemoveAt(i);
440 break;
441 }
442 }
443 }
444 }
445 }
446
447 /// <summary>
448 /// 删除节点的一个属性,当其属性值等于给定的值时
449 /// </summary>
450 /// <param >节点所在的xpath表达式</param>
451 /// <param >属性</param>
452 /// <param >值</param>
453 public void DeleteAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)
454 {
455 XmlNodeList nodePath = this.xmlDoc.SelectNodes(NodePath);
456 if (!(nodePath == null))
457 {
458 foreach (XmlNode tempxn in nodePath)
459 {
460 XmlAttributeCollection xmlAttr = tempxn.Attributes;
461 for (int i = 0; i < xmlAttr.Count; i++)
462 {
463 if (xmlAttr.Item(i).Name == NodeAttribute && xmlAttr.Item(i).Value == NodeAttributeValue)
464 {
465 tempxn.Attributes.RemoveAt(i);
466 break;
467 }
468 }
469 }
470 }
471 }
472 /// <summary>
473 /// 删除节点
474 /// </summary>
475 /// <param ></param>
476 /// <remarks></remarks>
477 public void DeleteXmlNode(string tempXmlNode)
478 {
479 XmlNodeList nodePath = this.xmlDoc.SelectNodes(tempXmlNode);
480 if (!(nodePath == null))
481 {
482 foreach (XmlNode xn in nodePath)
483 {
484 xn.ParentNode.RemoveChild(xn);
485 }
486 }
487 }
488
489 #endregion
490
491 #region XML文档事件----------------------------------------------------
492 /// <summary>
493 /// 节点插入事件
494 /// </summary>
495 /// <param ></param>
496 /// <param ></param>
497 private void nodeInsertEvent(Object src, XmlNodeChangedEventArgs args)
498 {
499 //保存设置
500 SaveXmlDocument();
501 }
502 /// <summary>
503 /// 节点删除事件
504 /// </summary>
505 /// <param ></param>
506 /// <param ></param>
507 private void nodeDeleteEvent(Object src, XmlNodeChangedEventArgs args)
508 {
509 //保存设置
510 SaveXmlDocument();
511 }
512 /// <summary>
513 /// 节点更新事件
514 /// </summary>
515 /// <param ></param>
516 /// <param ></param>
517 private void nodeUpdateEvent(Object src, XmlNodeChangedEventArgs args)
518 {
519 //保存设置
520 SaveXmlDocument();
521 }
522 #endregion
523
524 #region 保存XML文件----------------------------------------------------
525 /// <summary>
526 /// 功能:
527 /// 保存XML文件
528 /// </summary>
529 public void SaveXmlDocument()
530 {
531 try
532 {
533 //保存设置的结果
534 if (this.xmlFilePathType == enumXmlPathType.AbsolutePath)
535 {
536 Savexml(xmlFilePath);
537 }
538 else if (this.xmlFilePathType == enumXmlPathType.VirtualPath)
539 {
540 Savexml(HttpContext.Current.Server.MapPath(xmlFilePath));
541 }
542 }
543 catch (XmlException xmle)
544 {
545 throw xmle;
546 }
547 }
548
549 /// <summary>
550 /// 功能:
551 /// 保存XML文件
552 /// </summary>
553 public void SaveXmlDocument(string tempXMLFilePath)
554 {
555 try
556 {
557 //保存设置的结果
558 Savexml(tempXMLFilePath);
559 }
560 catch (XmlException xmle)
561 {
562 throw xmle;
563 }
564 }
565 /// <summary>
566 ///
567 /// </summary>
568 /// <param ></param>
569 private void Savexml(string filepath)
570 {
571 xmlDoc.Save(filepath);
572 }
573
574 #endregion
575
576 }
577
578}
579