XML操作实用类

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Xml;
  5 using System.Data;
  6 using System.IO;
  7 using System.Web;
  8 
  9 public class XMLHelper
 10     {
 11         #region 私有变量
 12         /// <summary>
 13         /// 文件路径
 14         /// </summary>
 15         private string xmlFilePath;
 16         /// <summary>
 17         /// 文件路径类型
 18         /// </summary>
 19         private enumXmlPathType xmlFilePathType;
 20         /// <summary>
 21         /// 实例化一个XmlDocument对象
 22         /// </summary>
 23         private XmlDocument xmlDoc = new XmlDocument();
 24         #endregion
 25 
 26         #region 私有函数
 27         /// <summary>
 28         /// 创建xml文件
 29         /// </summary>
 30         /// <param name="xmlName">文件路径</param>
 31         /// <returns></returns>
 32         private bool CreateXML(string xmlName)
 33         {
 34             try
 35             {
 36                 XmlTextWriter xmltx=null;
 37                 if (string.IsNullOrEmpty(xmlName))
 38                 {
 39                     xmltx = new XmlTextWriter(HttpContext.Current.Server.MapPath("Nimeux.xml"), Encoding.UTF8);
 40                 }
 41                 else
 42                 {
 43                     xmltx = new XmlTextWriter(xmlName, Encoding.UTF8);
 44                 }
 45                 xmltx.WriteStartDocument();
 46                 xmltx.WriteStartElement("Nimeux");
 47                 xmltx.WriteEndElement();
 48                 xmltx.WriteEndDocument();
 49                 xmltx.Close();                
 50                 return true;
 51             }
 52             catch { return false; }
 53         }
 54         /// <summary>
 55         /// 获取XmlDocument实例化对象
 56         /// </summary>
 57         /// <param name="strEntityTypeName">实体类的名称</param>
 58         /// <returns>指定的XML描述文件的路径</returns>
 59         private XmlDocument GetXmlDocument()
 60         {
 61             XmlDocument doc = null;
 62 
 63             if (this.xmlFilePathType == enumXmlPathType.AbsolutePath)
 64             {
 65                 doc = GetXmlDocumentFromFile(xmlFilePath);
 66             }
 67             else if (this.xmlFilePathType == enumXmlPathType.VirtualPath)
 68             {
 69                 doc = GetXmlDocumentFromFile(HttpContext.Current.Server.MapPath(xmlFilePath));
 70             }
 71             return doc;
 72         }
 73         /// <summary>
 74         /// 获取文件XML路径
 75         /// </summary>
 76         /// <param name="tempXmlFilePath"></param>
 77         /// <returns></returns>
 78         private XmlDocument GetXmlDocumentFromFile(string tempXmlFilePath)
 79         {
 80             try
 81             {
 82                 string xmlFileFullPath = tempXmlFilePath;
 83                 xmlDoc.Load(xmlFileFullPath);
 84                 return xmlDoc;
 85             }
 86             catch
 87             {
 88                 if (CreateXML(tempXmlFilePath))
 89                 {
 90                     return GetXmlDocumentFromFile(tempXmlFilePath);
 91                 }
 92                 else
 93                 {
 94                     return null;
 95                 }
 96             }
 97         }
 98         /// <summary>
 99         /// 获取根节点路径
100         /// </summary>
101         /// <returns></returns>
102         private string GetPath()
103         {
104             string root = xmlDoc.DocumentElement.Name;
105             string result = root + "[last()]";
106             return result;
107         }
108         #endregion
109 
110         #region 共有属性及枚举、方法
111         /// <summary>
112         /// 获取根节点路径
113         /// </summary>
114         public string Path
115         {
116             get { return GetPath(); }
117         }
118         /// <summary>
119         /// 获取根节点名称
120         /// </summary>
121         public string RootName
122         {
123             get { return xmlDoc.DocumentElement.Name; }
124         }
125         /// <summary>
126         /// 获取根节点
127         /// </summary>
128         public XmlNode Root
129         {
130             get { return xmlDoc.DocumentElement; }
131         }
132         /// <summary>
133         /// 枚举,XML文件的路径
134         /// </summary>
135         public enum enumXmlPathType
136         {
137             AbsolutePath,
138             VirtualPath
139         }
140         /// <summary>
141         /// 设置XML文件路径属性
142         /// </summary>
143         public string XmlFilePath
144         {
145             set
146             {
147                 xmlFilePath = value;
148             }
149         }
150         /// <summary>
151         /// 设置文件路径类型
152         /// </summary>
153         public enumXmlPathType XmlFilePathTyp
154         {
155             set
156             {
157                 xmlFilePathType = value;
158             }
159         }
160         #endregion
161 
162         #region 构造函数
163         /// <summary>
164         /// 构造函数
165         /// </summary>
166         /// <param name="tempXmlFilePath">XML文件相对路径</param>
167         public XMLHelper(string tempXmlFilePath)
168         {
169             this.xmlFilePathType = enumXmlPathType.VirtualPath;
170             this.xmlFilePath = tempXmlFilePath;
171             GetXmlDocument();
172         }
173         /// <summary>
174         /// 构造函数
175         /// </summary>
176         /// <param name="tempXmlFilePath">XML文件路径</param>
177         /// <param name="tempXmlFilePathType">文件路径类型</param>
178         public XMLHelper(string tempXmlFilePath, enumXmlPathType tempXmlFilePathType)
179         {
180             this.xmlFilePathType = tempXmlFilePathType;
181             this.xmlFilePath = tempXmlFilePath;
182             GetXmlDocument();
183         }
184         #endregion
185 
186         #region 读取指定节点的指定属性值
187         /// <summary>
188         /// 获取指定节点的属性
189         /// </summary>
190         /// <param name="NodeIndex">节点索引</param>
191         /// <param name="strNodePath">节点路径</param>
192         /// <param name="strAttribute">属性名称</param>
193         /// <returns></returns>
194         public string GetXmlNodeAttribute(int NodeIndex, string strNodePath, string strAttribute)
195         {
196             string strReturn = "";
197             try
198             {
199                 if (NodeIndex != -1)
200                 {
201                     //根据指定路径获取节点列表
202                     XmlNodeList ndlist = xmlDoc.SelectNodes(strNodePath);
203                     //获取指定节点属性
204                     XmlAttributeCollection xmlAttr = ndlist[NodeIndex].Attributes;
205                     if (xmlAttr.Count != 0)
206                     {
207                         //获取节点属性列表中的指定属性名称的属性值
208                         if (!string.IsNullOrEmpty(strAttribute))
209                         {
210                             for (int i = 0; i < xmlAttr.Count; i++)
211                             {
212                                 if (xmlAttr.Item(i).Name == strAttribute)
213                                     strReturn = xmlAttr.Item(i).Value;
214                             }
215                         }
216                         else
217                         {
218                             strReturn = xmlAttr.Item(0).Value;
219                         }
220                     }
221                     else
222                     {
223                         return "";
224                     }
225                 }
226                 else
227                 {
228                     //根据指定路径获取节点
229                     XmlNode xmlNode = xmlDoc.SelectSingleNode(strNodePath);
230                     //获取节点的属性
231                     XmlAttributeCollection xmlAttr = xmlNode.Attributes;
232                     if (xmlAttr.Count != 0)
233                     {
234                         //获取节点属性列表中的指定属性名称的属性值
235                         if (!string.IsNullOrEmpty(strAttribute))
236                         {
237                             for (int i = 0; i < xmlAttr.Count; i++)
238                             {
239                                 if (xmlAttr.Item(i).Name == strAttribute)
240                                     strReturn = xmlAttr.Item(i).Value;
241                             }
242                         }
243                         else
244                         {
245                             strReturn = xmlAttr.Item(0).Value;
246                         }
247                     }
248                     else
249                     {
250                         return "";
251                     }
252                 }
253             }
254             catch (XmlException xmle)
255             {
256                 throw xmle;
257             }
258             return strReturn;
259         }
260         /// <summary>
261         /// 获取指定节点的属性
262         /// </summary>
263         /// <param name="NodeIndex">节点索引</param>
264         /// <param name="strNodePath">节点路径</param>
265         /// <param name="AttributeIndex">属性索引</param>
266         /// <returns>属性值</returns>
267         public string GetXmlNodeAttribute(int NodeIndex, string strNodePath, int AttributeIndex)
268         {
269             string strReturn = "";
270             try
271             {
272                 if (NodeIndex != -1)
273                 {
274                     //根据指定路径获取节点列表
275                     XmlNodeList ndlist = xmlDoc.SelectNodes(strNodePath);
276                     //获取指定节点属性
277                     XmlAttributeCollection xmlAttr = ndlist[NodeIndex].Attributes;
278                     //获取节点属性列表中的指定的属性值
279                     strReturn = xmlAttr.Item(AttributeIndex).Value;
280                 }
281                 else
282                 {
283                     //根据指定路径获取节点
284                     XmlNode xmlNode = xmlDoc.SelectSingleNode(strNodePath);
285                     //获取节点的属性
286                     XmlAttributeCollection xmlAttr = xmlNode.Attributes;
287                     //获取节点属性列表中的指定属性名称的属性值
288                     strReturn = xmlAttr.Item(AttributeIndex).Value;
289                 }
290             }
291             catch (XmlException xmle)
292             {
293                 throw xmle;
294             }
295             return strReturn;
296         }
297         /// <summary>
298         /// 获取指定节点的属性
299         /// </summary>
300         /// <param name="NodeIndex">节点索引</param>
301         /// <param name="strNodePath">节点路径</param>
302         /// <returns>属性值</returns>
303         public string GetXmlNodeAttribute(int NodeIndex, string strNodePath)
304         {
305             return GetXmlNodeAttribute(NodeIndex, strNodePath, "");
306         }
307         /// <summary>
308         /// 获取节点的属性
309         /// </summary>
310         /// <param name="strNodePath">节点路径</param>
311         /// <returns>属性值</returns>
312         public string GetXmlNodeAttribute(string strNodePath)
313         {
314             return GetXmlNodeAttribute(-1, strNodePath);
315         }
316         /// <summary>
317         /// 获取节点的指定的属性值
318         /// </summary>
319         /// <param name="strNodePath">节点路径</param>
320         /// <param name="strAttributeName">属性名称</param>
321         /// <returns>属性值</returns>
322         public string GetXmlNodeAttribute(string strNodePath, string strAttributeName)
323         {
324             return GetXmlNodeAttribute(-1, strNodePath, strAttributeName);
325         }
326         /// <summary>
327         /// 获取节点的指定的属性值
328         /// </summary>
329         /// <param name="strNodePath">节点路径</param>
330         /// <param name="AttributeIndex">节点索引</param>
331         /// <returns>属性值</returns>
332         public string GetXmlNodeAttribute(string strNodePath, int AttributeIndex)
333         {
334             return GetXmlNodeAttribute(-1, strNodePath, AttributeIndex);
335         }
336         #endregion
337 
338         #region 设置节点的属性值
339         /// <summary>
340         /// 设置一个指定节点的指定属性的值
341         /// </summary>
342         /// <param name="NodeIndex">节点索引</param>
343         /// <param name="xmlNodePath">节点路径</param>
344         /// <param name="AttributeName">属性名称</param>
345         /// <param name="AttributeValue">属性值</param>
346         /// <returns></returns>
347         public bool SetXmlNodeAttribute(int NodeIndex, string xmlNodePath, string AttributeName, string AttributeValue)
348         {
349             string temp = string.Format(AttributeName);
350             try
351             {
352                 //是否有多个相同的节点
353                 if (NodeIndex != -1)
354                 {
355                     //获取节点列表
356                     XmlNodeList ndlist = xmlDoc.SelectNodes(xmlNodePath);
357                     //获取属性列表
358                     XmlAttributeCollection xmlAttr = ndlist[NodeIndex].Attributes;
359                     //属性是否为空
360                     if (xmlAttr != null)
361                     {
362                         //是否指定属性名称(也就是是否节点是唯一属性)
363                         if (!string.IsNullOrEmpty(AttributeName))
364                         {
365                             //获取属性相对应的属性名次,并设置属性名
366                             for (int i = 0; i < xmlAttr.Count; i++)
367                             {
368                                 if (xmlAttr.Item(i).Name == AttributeName)
369                                 {
370                                     xmlAttr.Item(i).Value = AttributeValue;
371                                     break;
372                                 }
373                             }
374                         }
375                         else
376                         {
377                             //没有制定属性名的情况下返回第一个属性
378                             xmlAttr.Item(0).Value = AttributeValue;
379                         }
380                         return true;
381                     }
382                     else
383                     {
384                         return false;
385                     }
386                 }
387                 else
388                 {
389                     //根据指定路径获取节点
390                     XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath);
391                     //获取节点的属性,并循环取出需要的属性值
392                     XmlAttributeCollection xmlAttr = xmlNode.Attributes;
393                     //属性是否为空
394                     if (xmlAttr != null)
395                     {
396                         //是否指定属性名称(也就是是否节点是唯一属性)
397                         if (!string.IsNullOrEmpty(AttributeName))
398                         {
399                             //获取属性相对应的属性名次,并设置属性名
400                             for (int i = 0; i < xmlAttr.Count; i++)
401                             {
402                                 if (xmlAttr.Item(i).Name == AttributeName)
403                                 {
404                                     xmlAttr.Item(i).Value = AttributeValue;
405                                     break;
406                                 }
407                             }
408                         }
409                         else
410                         {
411                             //没有制定属性名的情况下返回第一个属性
412                             xmlAttr.Item(0).Value = AttributeValue;
413                         }
414                         return true;
415                     }
416                     else
417                     {
418                         return true;
419                     }
420                 }
421             }
422             catch (XmlException xmle)
423             {
424                 throw xmle;
425             }
426         }
427         /// <summary>
428         /// 设置一个节点指定属性的值
429         /// </summary>
430         /// <param name="xmlNodePath">节点路径</param>
431         /// <param name="AttributeName">属性名称</param>
432         /// <param name="AttributeValue">属性值</param>
433         /// <returns>bool值</returns>
434         public bool SetXmlNodeAttribute(string xmlNodePath, string AttributeName, string AttributeValue)
435         {
436             return SetXmlNodeAttribute(-1, xmlNodePath, AttributeName, AttributeValue);
437         }
438         /// <summary>
439         /// 设置节点的唯一属性的值
440         /// </summary>
441         /// <param name="xmlNodePath">节点路径</param>
442         /// <param name="AttributeValue">属性值</param>
443         /// <returns></returns>
444         public bool SetXmlNodeAttribute(string xmlNodePath, string AttributeValue)
445         {
446             return SetXmlNodeAttribute(-1, xmlNodePath, "", AttributeValue);
447         }
448         #endregion
449 
450         #region 添加属性
451         /// <summary>
452         /// 向指定的一个节点添加指定值得属性
453         /// </summary>
454         /// <param name="NodeIndex">节点索引</param>
455         /// <param name="NodePath">节点路径</param>
456         /// <param name="AttributeName">添加的属性</param>
457         /// <param name="AttributeValue">要添加的属性值</param>
458         /// <returns>bool值</returns>
459         public bool AddAttribute(int NodeIndex, string NodePath, string AttributeName, string AttributeValue)
460         {
461             try
462             {
463                 //是否存在多个相同的节点名称
464                 if (NodeIndex != -1)
465                 {
466                     //获取节点列表
467                     XmlNodeList ndlist = xmlDoc.SelectNodes(NodePath);
468                     //创建一个属性
469                     XmlAttribute nodeAttri = xmlDoc.CreateAttribute(AttributeName);
470                     //如果属性值不为空,则添加属性值
471                     if (!string.IsNullOrEmpty(AttributeValue))
472                     {
473                         nodeAttri.Value = AttributeValue;
474                     }
475                     //将属性添加到节点
476                     ndlist[NodeIndex].Attributes.Append(nodeAttri);
477                     return true;
478                 }
479                 else
480                 {
481                     //获取节点
482                     XmlAttribute nodeAttri = xmlDoc.CreateAttribute(AttributeName);
483                     //如果属性值不为空,则添加属性值
484                     if (string.IsNullOrEmpty(AttributeValue))
485                     {
486                         nodeAttri.Value = AttributeValue;
487                     }
488                     XmlNode nodePath = xmlDoc.SelectSingleNode(NodePath);
489                     nodePath.Attributes.Append(nodeAttri);
490                     return true;
491                 }
492             }
493             catch { return false; }
494         }
495         /// <summary>
496         /// 给一个节点添加属性值
497         /// </summary>
498         /// <param name="NodePath">节点路径</param>
499         /// <param name="AttributeName">属性名称</param>
500         /// <returns>bool值</returns>
501         public bool AddAttribute(string NodePath, string AttributeName)
502         {
503             return AddAttribute(-1, NodePath, AttributeName);
504         }
505         /// <summary>
506         /// 给一个指定节点添加属性值
507         /// </summary>
508         /// <param name="NodeIndex">节点索引</param>
509         /// <param name="NodePath">节点路径</param>
510         /// <param name="AttributeName">属性名称</param>
511         /// <returns>bool值</returns>
512         public bool AddAttribute(int NodeIndex, string NodePath, string AttributeName)
513         {
514             return AddAttribute(NodeIndex, NodePath, AttributeName, "");
515         }
516         #endregion
517 
518         #region 删除属性
519         /// <summary>
520         /// 删除一个指定节点的指定值的属性
521         /// </summary>
522         /// <param name="NodeIndex">节点索引</param>
523         /// <param name="NodePath">节点路径</param>
524         /// <param name="AttributeName">属性名称</param>
525         /// <param name="AttributeValue">属性值</param>
526         /// <returns>bool值</returns>
527         public bool DeleteAttribute(int NodeIndex, string NodePath, string AttributeName, string AttributeValue)
528         {
529             try
530             {
531                 //是否有相同的节点名称
532                 if (NodeIndex != -1)
533                 {
534                     //获取节点列表
535                     XmlNodeList ndlist = xmlDoc.SelectNodes(NodePath);
536                     //获取指定列中的节点元素
537                     XmlElement xe = (XmlElement)ndlist[NodeIndex];
538                     //属性值是否为空
539                     if (!string.IsNullOrEmpty(AttributeValue))
540                     {
541                         //删除指定值得属性
542                         if (xe.GetAttribute(AttributeName) == AttributeValue)
543                         {
544                             xe.RemoveAttribute(AttributeName);
545                         }
546                     }
547                     else
548                     {
549                         xe.RemoveAttribute(AttributeName);
550                     }
551                     return true;
552                 }
553                 else
554                 {
555                     //获取节点
556                     XmlNode xn = xmlDoc.SelectSingleNode(NodePath);
557                     //获取列中的元素
558                     XmlElement xe = (XmlElement)xn;
559                     //属性值是否为空
560                     if (!string.IsNullOrEmpty(AttributeValue))
561                     {
562                         //删除指定值得属性
563                         if (xe.GetAttribute(AttributeName) == AttributeValue)
564                         {
565                             xe.RemoveAttribute(AttributeName);
566                         }
567                     }
568                     else
569                     {
570                         xe.RemoveAttribute(AttributeName);
571                     }
572                     return true;
573                 }
574             }
575             catch { return false; }
576         }
577         /// <summary>
578         /// 删除一个节点的指定值得属性
579         /// </summary>
580         /// <param name="NodePath">节点路径</param>
581         /// <param name="AttributeName">属性名称</param>
582         /// <param name="AttributeValue">属性值</param>
583         /// <returns>bool值</returns>
584         public bool DeleteAttribute(string NodePath, string AttributeName, string AttributeValue)
585         {
586             return DeleteAttribute(-1, NodePath, AttributeName, AttributeValue);
587         }
588         /// <summary>
589         /// 删除一个节点的一个属性
590         /// </summary>
591         /// <param name="NodePath">节点路径</param>
592         /// <param name="AttributeName">属性名称</param>
593         /// <returns>bool值</returns>
594         public bool DeleteAttribute(string NodePath, string AttributeName)
595         {
596             return DeleteAttribute(NodePath, AttributeName, "");
597         }
598         /// <summary>
599         /// 删除一个指定的节点的属性
600         /// </summary>
601         /// <param name="NodeIndex">节点索引</param>
602         /// <param name="NodePath">节点路径</param>
603         /// <param name="AttributeName">属性名</param>
604         /// <returns>bool值</returns>
605         public bool DeleteAttribute(int NodeIndex, string NodePath, string AttributeName)
606         {
607             return DeleteAttribute(NodeIndex, NodePath, AttributeName, "");
608         }
609         #endregion
610 
611         #region 获取节点值
612         /// <summary>
613         /// 获取指定节点的值
614         /// </summary>
615         /// <param name="Index">指定节点标记</param>
616         /// <param name="strNodePath">节点路径</param>
617         /// <param name="childNodeName">指定节点下子节点的名称</param>
618         /// <returns>bool值</returns>
619         public string GetXmlNodeValue(int Index, string strNodePath, string childNodeName)
620         {
621             string strReturn = String.Empty;
622             try
623             {
624                 if (!string.IsNullOrEmpty(childNodeName))
625                 {
626                     if (Index != -1)
627                     {
628                         //获取所有父节点下相同的节点并生成列表
629                         XmlNodeList ndlist = xmlDoc.SelectNodes(strNodePath + "/" + childNodeName);
630                         //获取列表中指定Index的节点值
631                         strReturn = ndlist[Index].InnerText;
632                     }
633                     else
634                     {
635                         //根据路径获取节点
636                         XmlNode xmlNode = xmlDoc.SelectSingleNode(strNodePath + "/" + childNodeName);
637                         strReturn = xmlNode.InnerText;
638                     }
639                 }
640                 else
641                 {
642                     if (Index != -1)
643                     {
644                         //获取所有父节点下相同的节点并生成列表
645                         XmlNodeList ndlist = xmlDoc.SelectNodes(strNodePath);
646                         //获取列表中指定Index的节点值
647                         strReturn = ndlist[Index].InnerText;
648                     }
649                     else
650                     {
651                         //根据路径获取节点
652                         XmlNode xmlNode = xmlDoc.SelectSingleNode(strNodePath);
653                         strReturn = xmlNode.InnerText;
654                     }
655                 }
656                 return strReturn;
657             }
658             catch
659             {
660                 return null;
661             }
662         }
663         /// <summary>
664         /// 获取节点的值
665         /// </summary>
666         /// <param name="strNodePath">节点路径</param>
667         /// <returns>bool值</returns>
668         public string GetXmlNodeValue(string strNodePath)
669         {
670             return GetXmlNodeValue(-1, strNodePath);
671         }
672         /// <summary>
673         /// 获取指定节点的值
674         /// </summary>
675         /// <param name="Index">指定节点标记</param>
676         /// <param name="strNodePath">节点路径</param>
677         /// <returns>bool值</returns>
678         public string GetXmlNodeValue(int Index, string strNodePath)
679         {
680             return GetXmlNodeValue(Index, strNodePath, "");
681         }
682         #endregion
683 
684         #region 设置节点值
685         /// <summary>
686         /// 设置节点值
687         /// </summary>
688         /// <param name="index">节点序号</param>
689         /// <param name="xmlNodePath">节点路径</param>
690         /// <param name="xmlNodeValue">节点值</param> 
691         /// <returns>bool值</returns>        
692         public bool SetXmlNodeValue(int index, string xmlNodePath, string xmlNodeValue)
693         {
694             try
695             {
696                 //如果没有指定Index的值,则设置一个节点的值
697                 if (index != -1)
698                 {
699                     //获取父节点下所有的相同节点
700                     XmlNodeList ndlist = xmlDoc.SelectNodes(xmlNodePath);
701                     //获取节点列表中的一个节点
702                     XmlNode xn = ndlist[index];
703                     //设置一个节点值
704                     xn.InnerText = xmlNodeValue;
705                 }
706                 else
707                 {
708                     //获取节点并设置一个值
709                     XmlNode xn = xmlDoc.SelectSingleNode(xmlNodePath);
710                     xn.InnerText = xmlNodeValue;
711                 }
712                 return true;
713             }
714             catch
715             {
716                 return false;
717             }
718         }
719         /// <summary>
720         /// 设置一个节点的值
721         /// </summary>
722         /// <param name="xmlNodePath">节点路径</param>
723         /// <param name="xmlNodeValue">节点值</param>
724         /// <returns>bool值</returns>
725         public bool SetXmlNodeValue(string xmlNodePath, string xmlNodeValue)
726         {
727             return SetXmlNodeValue(-1, xmlNodePath, xmlNodeValue);
728         }
729         #endregion
730 
731         #region 添加节点
732         /// <summary>
733         /// 在根节点下添加父节点
734         /// </summary>
735         /// <param name="parentNode">父节点名称</param>
736         public bool AddParentNode(string parentNode)
737         {
738             try
739             {
740                 XmlNode root = xmlDoc.DocumentElement;
741                 XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode);
742                 root.AppendChild(parentXmlNode);
743                 //SaveXmlDocument();
744                 return true;
745             }
746             catch { return false; }
747         }
748 
749         /// <summary>
750         /// 在指定父节点下插入一个指定数值、指定属性的子节点
751         /// </summary>
752         /// <param name="parentNodePath">父节点路径</param>
753         /// <param name="childNodeName">子节点名称</param>
754         /// <param name="nodevalue">子节点值</param>
755         /// <param name="nodeAttributeName">子节点属性名称</param>
756         /// <param name="nodeAttributeValue">子节点属性值</param>
757         /// <returns>bool值</returns>
758         public bool AddChildNode(string parentNodePath, string childNodeName, string nodevalue, string nodeAttributeName, string nodeAttributeValue)
759         {
760             try
761             {
762                 XmlNodeList ndlist = xmlDoc.SelectNodes(parentNodePath);
763                 int len = ndlist.Count;
764                 foreach (XmlNode xn in ndlist)
765                 {
766                     XmlNode childXmlNode = xmlDoc.CreateElement(childNodeName);
767                     xn.AppendChild(childXmlNode);
768                     XmlElement xnElm = (XmlElement)childXmlNode;
769                     if ((!string.IsNullOrEmpty(nodeAttributeName)) && (!string.IsNullOrEmpty(nodeAttributeValue)))
770                     {
771                         xnElm.SetAttribute(nodeAttributeName, nodeAttributeValue);
772                     }
773                     if (!string.IsNullOrEmpty(nodevalue))
774                     {
775                         xnElm.InnerText = nodevalue;
776                     }
777                 }
778                 //SaveXmlDocument();
779                 return true;
780             }
781             catch { return false; }
782         }
783         /// <summary>
784         /// 在指定的父节点下插入一个子节点
785         /// </summary>
786         /// <param name="childNodeName">父节点路径</param>
787         /// <param name="childNodeName">子节点名称</param>
788         /// <returns>bool值</returns>
789         public bool AddChildNode(string childNodePath, string childNodeName)
790         {
791             return AddChildNode(childNodePath, childNodeName, """""");
792         }
793         /// <summary>
794         /// 在指定的父节点下出入一个指定属性的子节点
795         /// </summary>
796         /// <param name="parentNodePath">父节点路径</param>
797         /// <param name="childNodeName">子节点名称</param>
798         /// <param name="nodeAttributeName">子节点属性名称</param>
799         /// <param name="nodeAttributeValue">子节点属性值</param>
800         /// <returns>bool值</returns>
801         public bool AddChildNode(string parentNodePath, string childNodeName, string nodeAttributeName, string nodeAttributeValue)
802         {
803             return AddChildNode(parentNodePath, childNodeName, "", nodeAttributeName, nodeAttributeValue);
804         }
805         /// <summary>
806         /// 在指定的父节点下插入一个指定值得子节点
807         /// </summary>
808         /// <param name="parentNodePath">父节点路径</param>
809         /// <param name="childNodeName">子节点名称</param>
810         /// <param name="nodevalue">之节点值</param>
811         /// <returns>bool值</returns>
812         public bool AddChildNode(string parentNodePath, string childNodeName, string nodevalue)
813         {
814             return AddChildNode(parentNodePath, childNodeName, nodevalue, """");
815         }
816         #endregion
817 
818         #region 删除节点
819         /// <summary>
820         /// 删除指定一个的节点及其下面的子节点
821         /// </summary>
822         /// <param name="NodeIndex">节点索引</param>
823         /// <param name="NodePath">节点路径</param>
824         /// <returns>bool值</returns>
825         public bool DeleteNode(int NodeIndex, string NodePath)
826         {
827             try
828             {
829                 //是否有多行,并指定删除哪一行
830                 if (NodeIndex != -1)
831                 {
832                     //获取节点列表
833                     XmlNodeList ndlist = xmlDoc.SelectNodes(NodePath);
834                     //获取要删除的节点
835                     XmlNode xn = (XmlNode)ndlist[NodeIndex];
836                     //删除节点
837                     xn.ParentNode.RemoveChild(xn);
838                 }
839                 else
840                 {
841                     //获取节点列表
842                     XmlNodeList ndlist = xmlDoc.SelectNodes(NodePath);
843                     //删除列表
844                     foreach (XmlNode xn in ndlist)
845                     {
846                         xn.ParentNode.RemoveChild(xn);
847                     }
848                 }
849             }
850             catch { return false; }
851             return true;
852         }
853         /// <summary>
854         /// 删除节点及节点下的子节点
855         /// </summary>
856         /// <param name="NodePath">节点路径</param>
857         /// <returns>bool值</returns>
858         public bool DeleteNode(string NodePath)
859         {
860             return DeleteNode(-1, NodePath);
861         }
862         #endregion
863 
864         #region 保存XML文件
865         /// <summary>
866         /// 保存文件
867         /// </summary>
868         public void SaveXmlDocument()
869         {
870             try
871             {
872                 //保存设置的结果
873                 xmlDoc.Save(HttpContext.Current.Server.MapPath(xmlFilePath));
874             }
875             catch (XmlException xmle)
876             {
877                 throw xmle;
878             }
879         }
880         /// <summary>
881         /// 保存文件
882         /// </summary>
883         /// <param name="tempXMLFilePath">文件路径</param>
884         public void SaveXmlDocument(string tempXMLFilePath)
885         {
886             try
887             {
888                 //保存设置的结果
889                 xmlDoc.Save(tempXMLFilePath);
890             }
891             catch (XmlException xmle)
892             {
893                 throw xmle;
894             }
895         }
896         #endregion
897     }
posted @ 2008-08-16 23:31  冰封的心  阅读(282)  评论(0)    收藏  举报