+.net操纵xml文件类(c#)+

一直想要写一个操作XML文件的类,今天在网上找了一下,找到一个已写的差不多的类,对其进行扩展与修改,最终成了以下代码,供新手参考参考.
//
在此类中用到了XML事件.此类中对于节点的查找必需用xpath表达式,如果你对xpath表达式不了解可以查看我收藏的另外一篇文章: +XML文件操作:[学习xpath]XPath最通俗的教程+
ContractedBlock.gif ExpandedBlockStart.gif
  1None.gifusing System;
  2None.gifusing System.Xml;
  3None.gifusing System.Web;
  4None.gifnamespace solucky
  5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  7InBlock.gif    /// 必需用XPATH表达式来获取相应节点
  8InBlock.gif    /// 关于xpath可以参见:
  9ExpandedSubBlockEnd.gif    /// </summary>

 10InBlock.gif    public class MyXml
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12ContractedSubBlock.gifExpandedSubBlockStart.gif        变量#region 变量
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 14InBlock.gif        /// xml文件所在路径类型
 15InBlock.gif        /// </summary>
 16ExpandedSubBlockEnd.gif        /// <remarks>xml文件所在路径类型</remarks>

 17InBlock.gif        public enum enumXmlPathType
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 19ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 20InBlock.gif            /// 绝对路径
 21ExpandedSubBlockEnd.gif            /// </summary>

 22InBlock.gif            AbsolutePath,
 23ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 24InBlock.gif            /// 虚拟路径
 25ExpandedSubBlockEnd.gif            /// </summary>

 26InBlock.gif            VirtualPath
 27ExpandedSubBlockEnd.gif        }

 28InBlock.gif
 29InBlock.gif        private string xmlFilePath ;
 30InBlock.gif        private enumXmlPathType xmlFilePathType ;
 31InBlock.gif        private XmlDocument xmlDoc = new XmlDocument() ;
 32ExpandedSubBlockEnd.gif        #endregion

 33InBlock.gif
 34InBlock.gif
 35ContractedSubBlock.gifExpandedSubBlockStart.gif        属性#region 属性
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 37InBlock.gif        /// 文件路径
 38InBlock.gif        /// </summary>
 39ExpandedSubBlockEnd.gif        /// <remarks>文件路径</remarks>

 40InBlock.gif        public string XmlFilePath
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 42InBlock.gif            get
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 44InBlock.gif                return this.xmlFilePath;
 45ExpandedSubBlockEnd.gif            }

 46InBlock.gif            set
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 48InBlock.gif                xmlFilePath = value ;
 49InBlock.gif
 50ExpandedSubBlockEnd.gif            }

 51ExpandedSubBlockEnd.gif        }

 52InBlock.gif
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 54InBlock.gif        /// 文件路径类型
 55ExpandedSubBlockEnd.gif        /// </summary>

 56InBlock.gif        public enumXmlPathType XmlFilePathTyp
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 58InBlock.gif            set
 59ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 60InBlock.gif                xmlFilePathType = value ;
 61ExpandedSubBlockEnd.gif            }

 62ExpandedSubBlockEnd.gif        }

 63ExpandedSubBlockEnd.gif        #endregion

 64InBlock.gif
 65ContractedSubBlock.gifExpandedSubBlockStart.gif        构造函数#region 构造函数
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 67InBlock.gif        /// 
 68InBlock.gif        /// </summary>
 69ExpandedSubBlockEnd.gif        /// <param name="tempXmlFilePath"></param>

 70InBlock.gif        public MyXml( string tempXmlFilePath )
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 72InBlock.gif            //
 73InBlock.gif            // TODO: 在此处添加构造函数逻辑
 74InBlock.gif            //
 75InBlock.gif
 76InBlock.gif            this.xmlFilePathType = enumXmlPathType.VirtualPath ;
 77InBlock.gif            this.xmlFilePath = tempXmlFilePath ;
 78InBlock.gif            GetXmlDocument() ;
 79InBlock.gif            //xmlDoc.Load( xmlFilePath ) ;
 80ExpandedSubBlockEnd.gif        }

 81InBlock.gif    
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 83InBlock.gif        /// 构造函数
 84InBlock.gif        /// </summary>
 85InBlock.gif        /// <param name="tempXmlFilePath">文件路径</param>
 86ExpandedSubBlockEnd.gif        /// <param name="tempXmlFilePathType">类型</param>

 87InBlock.gif        public MyXml( string tempXmlFilePath , enumXmlPathType tempXmlFilePathType )
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            //
 90InBlock.gif            // TODO: 在此处添加构造函数逻辑
 91InBlock.gif            //
 92InBlock.gif            this.xmlFilePathType = tempXmlFilePathType ;
 93InBlock.gif            this.xmlFilePath = tempXmlFilePath ;
 94InBlock.gif            GetXmlDocument() ;
 95ExpandedSubBlockEnd.gif        }

 96ExpandedSubBlockEnd.gif        #endregion

 97InBlock.gif
 98InBlock.gif
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////<summary>
100InBlock.gif        ///获取XmlDocument实体类
101InBlock.gif        ///</summary>    
102ExpandedSubBlockEnd.gif        /// <returns>指定的XML描述文件的一个xmldocument实例</returns>

103InBlock.gif        private XmlDocument GetXmlDocument()
104ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
105InBlock.gif            XmlDocument doc=null;
106InBlock.gif
107InBlock.gif            ifthis.xmlFilePathType == enumXmlPathType.AbsolutePath )
108ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
109InBlock.gif                doc = GetXmlDocumentFromFile( xmlFilePath ) ;
110ExpandedSubBlockEnd.gif            }

111InBlock.gif            else ifthis.xmlFilePathType == enumXmlPathType.VirtualPath )
112ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
113InBlock.gif                doc = GetXmlDocumentFromFile(HttpContext.Current.Server.MapPath(xmlFilePath)) ;
114ExpandedSubBlockEnd.gif            }

115InBlock.gif            return doc;
116ExpandedSubBlockEnd.gif        }

117InBlock.gif
118InBlock.gif        private XmlDocument GetXmlDocumentFromFile(string tempXmlFilePath)
119ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
120InBlock.gif            string xmlFileFullPath = tempXmlFilePath ;
121InBlock.gif            xmlDoc.Load(xmlFileFullPath) ;
122InBlock.gif            //定义事件处理
123InBlock.gif            xmlDoc.NodeChanged += new XmlNodeChangedEventHandler(this.nodeUpdateEvent);
124InBlock.gif            xmlDoc.NodeInserted += new XmlNodeChangedEventHandler(this.nodeInsertEvent);
125InBlock.gif            xmlDoc.NodeRemoved += new XmlNodeChangedEventHandler(this.nodeDeleteEvent);
126InBlock.gif            return xmlDoc ;
127ExpandedSubBlockEnd.gif        }

128InBlock.gif
129ContractedSubBlock.gifExpandedSubBlockStart.gif        读取指定节点的指定属性值#region 读取指定节点的指定属性值
130ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
131InBlock.gif        /// 功能:
132InBlock.gif        /// 读取指定节点的指定属性值    
133InBlock.gif        /// </summary>
134InBlock.gif        /// <param name="strNode">节点名称</param>
135InBlock.gif        /// <param name="strAttribute">此节点的属性</param>
136ExpandedSubBlockEnd.gif        /// <returns></returns>

137InBlock.gif        public string GetXmlNodeAttributeValue(string strNode,string strAttribute)
138ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
139InBlock.gif            string strReturn = "";
140InBlock.gif            try
141ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
142InBlock.gif                //根据指定路径获取节点
143InBlock.gif                XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode) ;
144InBlock.gif                if (!(xmlNode==null))
145ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{//获取节点的属性,并循环取出需要的属性值
146InBlock.gif                    XmlAttributeCollection xmlAttr = xmlNode.Attributes ;
147InBlock.gif
148InBlock.gif                    for(int i=0 ;i<xmlAttr.Count; i++)
149ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
150InBlock.gif                        if (xmlAttr.Item(i).Name == strAttribute)
151ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
152InBlock.gif                            strReturn = xmlAttr.Item(i).Value ;
153InBlock.gif                            break;
154ExpandedSubBlockEnd.gif                        }

155ExpandedSubBlockEnd.gif                    }

156ExpandedSubBlockEnd.gif                }

157ExpandedSubBlockEnd.gif            }

158InBlock.gif            catch(XmlException xmle)
159ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
160InBlock.gif                throw xmle ;
161ExpandedSubBlockEnd.gif            }

162InBlock.gif            return strReturn ;
163ExpandedSubBlockEnd.gif        }

164ExpandedSubBlockEnd.gif        #endregion
 
165InBlock.gif
166InBlock.gif
167ContractedSubBlock.gifExpandedSubBlockStart.gif        读取指定节点的值#region 读取指定节点的值
168ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
169InBlock.gif        /// 功能:
170InBlock.gif        /// 读取指定节点的值    
171InBlock.gif        /// </summary>
172InBlock.gif        /// <param name="strNode">节点名称</param>
173ExpandedSubBlockEnd.gif        /// <returns></returns>

174InBlock.gif        public string GetXmlNodeValue(string strNode)
175ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
176InBlock.gif            string strReturn = String.Empty ;
177InBlock.gif
178InBlock.gif            try
179ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
180InBlock.gif                //根据路径获取节点
181InBlock.gif                XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode) ;
182InBlock.gif                if (!(xmlNode==null))
183InBlock.gif                    strReturn = xmlNode.InnerText ;
184ExpandedSubBlockEnd.gif            }

185InBlock.gif            catch(XmlException xmle)
186ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
187InBlock.gif                throw xmle ;
188ExpandedSubBlockEnd.gif            }

189InBlock.gif            return strReturn ;
190ExpandedSubBlockEnd.gif        }

191ExpandedSubBlockEnd.gif        #endregion

192InBlock.gif
193ContractedSubBlock.gifExpandedSubBlockStart.gif        设置节点值#region 设置节点值
194ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
195InBlock.gif        /// 功能:
196InBlock.gif        /// 设置节点值        
197InBlock.gif        /// </summary>
198InBlock.gif        /// <param name="strNode">节点的名称</param>
199ExpandedSubBlockEnd.gif        /// <param name="newValue">节点值</param>

200InBlock.gif        public void SetXmlNodeValue(string xmlNodePath,string xmlNodeValue)
201ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
202InBlock.gif            try
203ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
204InBlock.gif                //可以批量为符合条件的节点进行付值
205InBlock.gif                XmlNodeList xmlNode=this.xmlDoc.SelectNodes(xmlNodePath);
206InBlock.gif                if (!(xmlNode==null))
207ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
208InBlock.gif                    foreach(XmlNode xn in xmlNode)
209ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
210InBlock.gif                        xn.InnerText = xmlNodeValue ;    
211ExpandedSubBlockEnd.gif                    }

212ExpandedSubBlockEnd.gif                }

213ExpandedSubBlockStart.gifContractedSubBlock.gif                /**//*
214InBlock.gif                 * 根据指定路径获取节点
215InBlock.gif                XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;            
216InBlock.gif                //设置节点值
217InBlock.gif                if (!(xmlNode==null))
218ExpandedSubBlockEnd.gif                    xmlNode.InnerText = xmlNodeValue ;*/
                
219ExpandedSubBlockEnd.gif            }

220InBlock.gif            catch(XmlException xmle)
221ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
222InBlock.gif                throw xmle ;
223ExpandedSubBlockEnd.gif            }

224ExpandedSubBlockEnd.gif        }

225ExpandedSubBlockEnd.gif        #endregion

226InBlock.gif
227ContractedSubBlock.gifExpandedSubBlockStart.gif        设置节点的属性值#region 设置节点的属性值
228ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
229InBlock.gif        /// 功能:
230InBlock.gif        /// 设置节点的属性值    
231InBlock.gif        /// </summary>
232InBlock.gif        /// <param name="xmlNodePath">节点名称</param>
233InBlock.gif        /// <param name="xmlNodeAttribute">属性名称</param>
234ExpandedSubBlockEnd.gif        /// <param name="xmlNodeAttributeValue">属性值</param>

235InBlock.gif        public void SetXmlNodeAttributeValue(string xmlNodePath,string xmlNodeAttribute,string xmlNodeAttributeValue)
236ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
237InBlock.gif            try
238ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
239InBlock.gif                //可以批量为符合条件的节点的属性付值
240InBlock.gif                XmlNodeList xmlNode=this.xmlDoc.SelectNodes(xmlNodePath);
241InBlock.gif                if (!(xmlNode==null))
242ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
243InBlock.gif                    foreach(XmlNode xn in xmlNode)
244ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
245InBlock.gif                        XmlAttributeCollection xmlAttr = xn.Attributes ;
246InBlock.gif                        for(int i=0 ; i<xmlAttr.Count ; i++)
247ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
248InBlock.gif                            if ( xmlAttr.Item(i).Name == xmlNodeAttribute )
249ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
250InBlock.gif                                xmlAttr.Item(i).Value = xmlNodeAttributeValue;
251InBlock.gif                                break ;
252ExpandedSubBlockEnd.gif                            }

253ExpandedSubBlockEnd.gif                        }
    
254ExpandedSubBlockEnd.gif                    }

255ExpandedSubBlockEnd.gif                }

256ExpandedSubBlockStart.gifContractedSubBlock.gif                /**//*单个节点
257InBlock.gif                //根据指定路径获取节点
258InBlock.gif                XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;
259InBlock.gif                if (!(xmlNode==null))
260InBlock.gif                {//获取节点的属性,并循环取出需要的属性值
261InBlock.gif                    XmlAttributeCollection xmlAttr = xmlNode.Attributes ;
262InBlock.gif                    for(int i=0 ; i<xmlAttr.Count ; i++)
263InBlock.gif                    {
264InBlock.gif                        if ( xmlAttr.Item(i).Name == xmlNodeAttribute )
265InBlock.gif                        {
266InBlock.gif                            xmlAttr.Item(i).Value = xmlNodeAttributeValue;
267InBlock.gif                            break ;
268InBlock.gif                        }
269InBlock.gif                    }    
270InBlock.gif                }
271ExpandedSubBlockEnd.gif                */

272ExpandedSubBlockEnd.gif            }

273InBlock.gif            catch(XmlException xmle)
274ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
275InBlock.gif                throw xmle ;
276ExpandedSubBlockEnd.gif            }

277ExpandedSubBlockEnd.gif        }

278ExpandedSubBlockEnd.gif        #endregion

279InBlock.gif
280ContractedSubBlock.gifExpandedSubBlockStart.gif        添加#region 添加
281ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
282InBlock.gif        /// 获取XML文件的根元素
283ExpandedSubBlockEnd.gif        /// </summary>

284InBlock.gif        public XmlNode GetXmlRoot()
285ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
286InBlock.gif            return xmlDoc.DocumentElement ;
287ExpandedSubBlockEnd.gif        }

288InBlock.gif
289ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
290InBlock.gif        /// 在根节点下添加父节点
291ExpandedSubBlockEnd.gif        /// </summary>

292InBlock.gif        public void AddParentNode(string parentNode)
293ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
294InBlock.gif            try
295ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
296InBlock.gif                XmlNode root = GetXmlRoot() ;
297InBlock.gif                XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode) ;
298InBlock.gif                root.AppendChild(parentXmlNode) ;                
299ExpandedSubBlockEnd.gif            }

300InBlock.gif            catch(XmlException xmle)
301ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
302InBlock.gif                throw xmle ;
303ExpandedSubBlockEnd.gif            }

304ExpandedSubBlockEnd.gif        }

305InBlock.gif        
306ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
307InBlock.gif        /// 向一个已经存在的父节点中插入一个子节点
308InBlock.gif        /// </summary>
309InBlock.gif        /// <param name="parentNodePath">父节点</param>
310ExpandedSubBlockEnd.gif        /// <param name="childNodePath">字节点名称</param>

311InBlock.gif        public void AddChildNode( string parentNodePath,string childnodename )
312ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
313InBlock.gif            try
314ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
315InBlock.gif                XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath) ;                
316InBlock.gif                if(!((parentXmlNode)==null))//如果此节点存在
317ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{    
318InBlock.gif                    XmlNode childXmlNode =  xmlDoc.CreateElement(childnodename) ;                
319InBlock.gif                    parentXmlNode.AppendChild( childXmlNode ) ;    
320ExpandedSubBlockEnd.gif                }

321ExpandedSubBlockStart.gifContractedSubBlock.gif                elsedot.gif{//如果不存在就放父节点添加
322InBlock.gif                    //this.GetXmlRoot().AppendChild(childXmlNode);
323ExpandedSubBlockEnd.gif                }

324InBlock.gif                            
325ExpandedSubBlockEnd.gif            }

326InBlock.gif            catch(XmlException xmle)
327ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
328InBlock.gif                throw xmle;
329ExpandedSubBlockEnd.gif            }

330ExpandedSubBlockEnd.gif        }

331InBlock.gif        
332ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
333InBlock.gif        /// 向一个节点添加属性
334InBlock.gif        /// </summary>
335InBlock.gif        /// <param name="NodePath">节点路径</param>
336ExpandedSubBlockEnd.gif        /// <param name="NodeAttribute">属性名</param>

337InBlock.gif        public void AddAttribute( string NodePath , string NodeAttribute)
338ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
339InBlock.gif            privateAddAttribute(NodePath,NodeAttribute,"");
340ExpandedSubBlockEnd.gif        }

341ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
342InBlock.gif        /// 
343InBlock.gif        /// </summary>
344InBlock.gif        /// <param name="NodePath"></param>
345InBlock.gif        /// <param name="NodeAttribute"></param>
346ExpandedSubBlockEnd.gif        /// <param name="NodeAttributeValue"></param>

347InBlock.gif        private void privateAddAttribute( string NodePath , string NodeAttribute,string NodeAttributeValue)
348ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
349InBlock.gif            try
350ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
351InBlock.gif                XmlNode nodePath = xmlDoc.SelectSingleNode( NodePath ) ;
352InBlock.gif                if (!(nodePath==null))
353ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{    
354InBlock.gif                    XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute); 
355InBlock.gif                    nodeAttribute.Value=NodeAttributeValue;
356InBlock.gif                    nodePath.Attributes.Append(nodeAttribute) ;                        
357ExpandedSubBlockEnd.gif                }

358ExpandedSubBlockEnd.gif            }

359InBlock.gif            catch(XmlException xmle)
360ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
361InBlock.gif                throw xmle;
362ExpandedSubBlockEnd.gif            }

363ExpandedSubBlockEnd.gif        }

364ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
365InBlock.gif        ///  向一个节点添加属性,并付值
366InBlock.gif        /// </summary>
367InBlock.gif        /// <param name="NodePath">节点</param>
368InBlock.gif        /// <param name="NodeAttribute">属性名</param>
369ExpandedSubBlockEnd.gif        /// <param name="NodeAttributeValue">属性值</param>

370InBlock.gif        public void AddAttribute( string NodePath , string NodeAttribute,string NodeAttributeValue)
371ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
372InBlock.gif            privateAddAttribute(NodePath,NodeAttribute,NodeAttributeValue);
373ExpandedSubBlockEnd.gif        }

374ExpandedSubBlockEnd.gif        #endregion

375InBlock.gif
376ContractedSubBlock.gifExpandedSubBlockStart.gif        删除#region 删除
377ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
378InBlock.gif        /// 删除节点的一个属性
379InBlock.gif        /// </summary>
380InBlock.gif        /// <param name="NodePath">节点所在的xpath表达式</param>
381ExpandedSubBlockEnd.gif        /// <param name="NodeAttribute">属性名</param>

382InBlock.gif        public void DeleteAttribute( string NodePath , string NodeAttribute)
383ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
384InBlock.gif            XmlNodeList nodePath =this.xmlDoc.SelectNodes(NodePath);            
385InBlock.gif            if (!(nodePath==null))
386ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
387InBlock.gif                foreach (XmlNode tempxn in nodePath)
388ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
389InBlock.gif                    XmlAttributeCollection xmlAttr = tempxn.Attributes ;
390InBlock.gif                    for(int i=0 ; i<xmlAttr.Count ; i++)
391ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
392InBlock.gif                        if ( xmlAttr.Item(i).Name == NodeAttribute)
393ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
394InBlock.gif                            tempxn.Attributes.RemoveAt(i);
395InBlock.gif                            break ;
396ExpandedSubBlockEnd.gif                        }

397ExpandedSubBlockEnd.gif                    }

398ExpandedSubBlockEnd.gif                }

399ExpandedSubBlockEnd.gif            }

400ExpandedSubBlockEnd.gif        }

401InBlock.gif        
402ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
403InBlock.gif        /// 删除节点,当其属性值等于给定的值时
404InBlock.gif        /// </summary>
405InBlock.gif        /// <param name="NodePath">节点所在的xpath表达式</param>
406InBlock.gif        /// <param name="NodeAttribute">属性</param>
407ExpandedSubBlockEnd.gif        /// <param name="NodeAttributeValue"></param>

408InBlock.gif        public void DeleteAttribute( string NodePath , string NodeAttribute , string NodeAttributeValue)
409ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
410InBlock.gif            XmlNodeList nodePath =this.xmlDoc.SelectNodes(NodePath);            
411InBlock.gif            if (!(nodePath==null))
412ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
413InBlock.gif                foreach (XmlNode tempxn in nodePath)
414ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
415InBlock.gif                    XmlAttributeCollection xmlAttr = tempxn.Attributes ;
416InBlock.gif                    for(int i=0 ; i<xmlAttr.Count ; i++)
417ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
418InBlock.gif                        if ( xmlAttr.Item(i).Name == NodeAttribute && xmlAttr.Item(i).Value==NodeAttributeValue)
419ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
420InBlock.gif                            tempxn.Attributes.RemoveAt(i);
421InBlock.gif                            break ;
422ExpandedSubBlockEnd.gif                        }

423ExpandedSubBlockEnd.gif                    }

424ExpandedSubBlockEnd.gif                }

425ExpandedSubBlockEnd.gif            }

426ExpandedSubBlockEnd.gif        }

427ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
428InBlock.gif        /// 删除节点
429InBlock.gif        /// </summary>
430InBlock.gif        /// <param name="tempXmlNode"></param>
431ExpandedSubBlockEnd.gif        /// <remarks></remarks>

432ExpandedSubBlockStart.gifContractedSubBlock.gif        public void DeleteXmlNode(string tempXmlNode)dot.gif{    
433InBlock.gif            XmlNodeList nodePath =this.xmlDoc.SelectNodes(tempXmlNode);
434InBlock.gif            if (!(nodePath==null))
435ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
436InBlock.gif                foreach(XmlNode xn in nodePath)
437ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
438InBlock.gif                    xn.ParentNode.RemoveChild(xn);        
439ExpandedSubBlockEnd.gif                }

440ExpandedSubBlockEnd.gif            }

441ExpandedSubBlockEnd.gif        }

442InBlock.gif
443ExpandedSubBlockEnd.gif        #endregion

444InBlock.gif
445ContractedSubBlock.gifExpandedSubBlockStart.gif        XML文档事件#region XML文档事件
446ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
447InBlock.gif        /// 
448InBlock.gif        /// </summary>
449InBlock.gif        /// <param name="src"></param>
450ExpandedSubBlockEnd.gif        /// <param name="args"></param>

451InBlock.gif        private  void nodeInsertEvent(Object src, XmlNodeChangedEventArgs args)
452ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
453InBlock.gif            //保存设置
454InBlock.gif            SaveXmlDocument();
455ExpandedSubBlockEnd.gif        }

456ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
457InBlock.gif        /// 
458InBlock.gif        /// </summary>
459InBlock.gif        /// <param name="src"></param>
460ExpandedSubBlockEnd.gif        /// <param name="args"></param>

461InBlock.gif        private  void nodeDeleteEvent(Object src, XmlNodeChangedEventArgs args)
462ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
463InBlock.gif            //保存设置
464InBlock.gif            SaveXmlDocument();
465ExpandedSubBlockEnd.gif        }

466ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
467InBlock.gif        /// 
468InBlock.gif        /// </summary>
469InBlock.gif        /// <param name="src"></param>
470ExpandedSubBlockEnd.gif        /// <param name="args"></param>

471InBlock.gif        private  void nodeUpdateEvent(Object src, XmlNodeChangedEventArgs args)
472ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
473InBlock.gif            //保存设置
474InBlock.gif            SaveXmlDocument();
475ExpandedSubBlockEnd.gif        }

476ExpandedSubBlockEnd.gif        #endregion

477InBlock.gif
478ContractedSubBlock.gifExpandedSubBlockStart.gif        保存XML文件#region 保存XML文件
479ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
480InBlock.gif        /// 功能: 
481InBlock.gif        /// 保存XML文件
482InBlock.gif        /// 
483ExpandedSubBlockEnd.gif        /// </summary>

484InBlock.gif        public void SaveXmlDocument()
485ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
486InBlock.gif            try
487ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
488InBlock.gif                //保存设置的结果
489InBlock.gif                ifthis.xmlFilePathType == enumXmlPathType.AbsolutePath )
490ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
491InBlock.gif                    Savexml( xmlFilePath ) ;
492ExpandedSubBlockEnd.gif                }

493InBlock.gif                else ifthis.xmlFilePathType == enumXmlPathType.VirtualPath )
494ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
495InBlock.gif                    Savexml(HttpContext.Current.Server.MapPath(xmlFilePath)) ;
496ExpandedSubBlockEnd.gif                }

497ExpandedSubBlockEnd.gif            }

498InBlock.gif            catch(XmlException xmle)
499ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
500InBlock.gif                throw xmle;
501ExpandedSubBlockEnd.gif            }

502ExpandedSubBlockEnd.gif        }

503InBlock.gif    
504ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
505InBlock.gif        /// 功能: 
506InBlock.gif        /// 保存XML文件    
507ExpandedSubBlockEnd.gif        /// </summary>

508InBlock.gif        public void SaveXmlDocument(string tempXMLFilePath)
509ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
510InBlock.gif            try
511ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
512InBlock.gif                //保存设置的结果
513InBlock.gif                Savexml(tempXMLFilePath);
514ExpandedSubBlockEnd.gif            }

515InBlock.gif            catch(XmlException xmle)
516ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
517InBlock.gif                throw xmle;
518ExpandedSubBlockEnd.gif            }

519ExpandedSubBlockEnd.gif        }

520ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
521InBlock.gif        /// 
522InBlock.gif        /// </summary>
523ExpandedSubBlockEnd.gif        /// <param name="filepath"></param>

524InBlock.gif        private void Savexml(string filepath)
525ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
526InBlock.gif            xmlDoc.Save(filepath);
527ExpandedSubBlockEnd.gif        }

528InBlock.gif
529ExpandedSubBlockEnd.gif        #endregion
 
530InBlock.gif
531ExpandedSubBlockEnd.gif    }

532InBlock.gif
533ExpandedBlockEnd.gif}

534None.gif
535None.gif

转载于:https://www.cnblogs.com/solucky/archive/2006/09/06/496635.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值