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

None.gif 1 using  System;
None.gif  2
using  System.Xml;
None.gif  3
using  System.Web;
None.gif  4
namespace  solucky
ExpandedBlockStart.gifContractedBlock.gif  
5 dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
6    /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif  7    /**//// 必需用XPATH表达式来获取相应节点
ExpandedSubBlockStart.gifContractedSubBlock.gif  8    /**//// 关于xpath可以参见:
ExpandedSubBlockStart.gifContractedSubBlock.gif  9    /**//// </summary>
InBlock.gif 10    public class MyXml
ExpandedSubBlockStart.gifContractedSubBlock.gif 
11    dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif 
12        变量变量#region 变量
ExpandedSubBlockStart.gifContractedSubBlock.gif 
13        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 14        /**//// xml文件所在路径类型
ExpandedSubBlockStart.gifContractedSubBlock.gif 15        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 16        /**//// <remarks>xml文件所在路径类型</remarks>
InBlock.gif 17        public enum enumXmlPathType
ExpandedSubBlockStart.gifContractedSubBlock.gif 
18        dot.gif{    
ExpandedSubBlockStart.gifContractedSubBlock.gif 
19            /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 20            /**//// 绝对路径
ExpandedSubBlockStart.gifContractedSubBlock.gif 21            /**//// </summary>
InBlock.gif 22            AbsolutePath,
ExpandedSubBlockStart.gifContractedSubBlock.gif 
23            /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 24            /**//// 虚拟路径
ExpandedSubBlockStart.gifContractedSubBlock.gif 25            /**//// </summary>
InBlock.gif 26            VirtualPath
ExpandedSubBlockEnd.gif 
27        }

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

InBlock.gif 
33
InBlock.gif 
34
ContractedSubBlock.gifExpandedSubBlockStart.gif 
35        属性属性#region 属性
ExpandedSubBlockStart.gifContractedSubBlock.gif 
36        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 37        /**//// 文件路径
ExpandedSubBlockStart.gifContractedSubBlock.gif 38        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 39        /**//// <remarks>文件路径</remarks>
InBlock.gif 40        public string XmlFilePath
ExpandedSubBlockStart.gifContractedSubBlock.gif 
41        dot.gif{
InBlock.gif 
42            get
ExpandedSubBlockStart.gifContractedSubBlock.gif 
43            dot.gif{
InBlock.gif 
44                return this.xmlFilePath;
ExpandedSubBlockEnd.gif 
45            }

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

ExpandedSubBlockEnd.gif 
51        }

InBlock.gif 
52
ExpandedSubBlockStart.gifContractedSubBlock.gif 
53        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 54        /**//// 文件路径类型
ExpandedSubBlockStart.gifContractedSubBlock.gif 55        /**//// </summary>
InBlock.gif 56        public enumXmlPathType XmlFilePathTyp
ExpandedSubBlockStart.gifContractedSubBlock.gif 
57        dot.gif{
InBlock.gif 
58            set
ExpandedSubBlockStart.gifContractedSubBlock.gif 
59            dot.gif{
InBlock.gif 
60                xmlFilePathType = value ;
ExpandedSubBlockEnd.gif 
61            }

ExpandedSubBlockEnd.gif 
62        }

ExpandedSubBlockEnd.gif 
63        #endregion

InBlock.gif 
64
ContractedSubBlock.gifExpandedSubBlockStart.gif 
65        构造函数构造函数#region 构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif 
66        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 67        /**//// 
ExpandedSubBlockStart.gifContractedSubBlock.gif 68        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 69        /**//// <param name="tempXmlFilePath"></param>
InBlock.gif 70        public MyXml( string tempXmlFilePath )
ExpandedSubBlockStart.gifContractedSubBlock.gif 
71        dot.gif{
InBlock.gif 
72            //
InBlock.gif
 73            // TODO: 在此处添加构造函数逻辑
InBlock.gif
 74            //
InBlock.gif
 75
InBlock.gif 
76            this.xmlFilePathType = enumXmlPathType.VirtualPath ;
InBlock.gif 
77            this.xmlFilePath = tempXmlFilePath ;
InBlock.gif 
78            GetXmlDocument() ;
InBlock.gif 
79            //xmlDoc.Load( xmlFilePath ) ;
ExpandedSubBlockEnd.gif
 80        }

InBlock.gif 
81    
ExpandedSubBlockStart.gifContractedSubBlock.gif 
82        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 83        /**//// 构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif 84        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif 85        /**//// <param name="tempXmlFilePath">文件路径</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif 86        /**//// <param name="tempXmlFilePathType">类型</param>
InBlock.gif 87        public MyXml( string tempXmlFilePath , enumXmlPathType tempXmlFilePathType )
ExpandedSubBlockStart.gifContractedSubBlock.gif 
88        dot.gif{
InBlock.gif 
89            //
InBlock.gif
 90            // TODO: 在此处添加构造函数逻辑
InBlock.gif
 91            //
InBlock.gif
 92            this.xmlFilePathType = tempXmlFilePathType ;
InBlock.gif 
93            this.xmlFilePath = tempXmlFilePath ;
InBlock.gif 
94            GetXmlDocument() ;
ExpandedSubBlockEnd.gif 
95        }

ExpandedSubBlockEnd.gif 
96        #endregion

InBlock.gif 
97
InBlock.gif 
98
ExpandedSubBlockStart.gifContractedSubBlock.gif 
99        /**//**//**////<summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif100        /**////获取XmlDocument实体类
ExpandedSubBlockStart.gifContractedSubBlock.gif101        /**////</summary>    
ExpandedSubBlockStart.gifContractedSubBlock.gif102        /**//// <returns>指定的XML描述文件的一个xmldocument实例</returns>
InBlock.gif103        private XmlDocument GetXmlDocument()
ExpandedSubBlockStart.gifContractedSubBlock.gif
104        dot.gif{
InBlock.gif
105            XmlDocument doc=null;
InBlock.gif
106
InBlock.gif
107            ifthis.xmlFilePathType == enumXmlPathType.AbsolutePath )
ExpandedSubBlockStart.gifContractedSubBlock.gif
108            dot.gif{
InBlock.gif
109                doc = GetXmlDocumentFromFile( xmlFilePath ) ;
ExpandedSubBlockEnd.gif
110            }

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

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

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

InBlock.gif
128
ContractedSubBlock.gifExpandedSubBlockStart.gif
129        读取指定节点的指定属性值读取指定节点的指定属性值#region 读取指定节点的指定属性值
ExpandedSubBlockStart.gifContractedSubBlock.gif
130        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif131        /**//// 功能:
ExpandedSubBlockStart.gifContractedSubBlock.gif132        /**//// 读取指定节点的指定属性值    
ExpandedSubBlockStart.gifContractedSubBlock.gif133        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif134        /**//// <param name="strNode">节点名称</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif135        /**//// <param name="strAttribute">此节点的属性</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif136        /**//// <returns></returns>
InBlock.gif137        public string GetXmlNodeAttributeValue(string strNode,string strAttribute)
ExpandedSubBlockStart.gifContractedSubBlock.gif
138        dot.gif{
InBlock.gif
139            string strReturn = "";
InBlock.gif
140            try
ExpandedSubBlockStart.gifContractedSubBlock.gif
141            dot.gif{
InBlock.gif
142                //根据指定路径获取节点
InBlock.gif
143                XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode) ;
InBlock.gif
144                if (!(xmlNode==null))
ExpandedSubBlockStart.gifContractedSubBlock.gif
145                dot.gif{//获取节点的属性,并循环取出需要的属性值
InBlock.gif
146                    XmlAttributeCollection xmlAttr = xmlNode.Attributes ;
InBlock.gif
147
InBlock.gif
148                    for(int i=0 ;i<xmlAttr.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
149                    dot.gif{
InBlock.gif
150                        if (xmlAttr.Item(i).Name == strAttribute)
ExpandedSubBlockStart.gifContractedSubBlock.gif
151                        dot.gif{
InBlock.gif
152                            strReturn = xmlAttr.Item(i).Value ;
InBlock.gif
153                            break;
ExpandedSubBlockEnd.gif
154                        }

ExpandedSubBlockEnd.gif
155                    }

ExpandedSubBlockEnd.gif
156                }

ExpandedSubBlockEnd.gif
157            }

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

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

ExpandedSubBlockEnd.gif
164        #endregion
 
InBlock.gif
165
InBlock.gif
166
ContractedSubBlock.gifExpandedSubBlockStart.gif
167        读取指定节点的值读取指定节点的值#region 读取指定节点的值
ExpandedSubBlockStart.gifContractedSubBlock.gif
168        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif169        /**//// 功能:
ExpandedSubBlockStart.gifContractedSubBlock.gif170        /**//// 读取指定节点的值    
ExpandedSubBlockStart.gifContractedSubBlock.gif171        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif172        /**//// <param name="strNode">节点名称</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif173        /**//// <returns></returns>
InBlock.gif174        public string GetXmlNodeValue(string strNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif
175        dot.gif{
InBlock.gif
176            string strReturn = String.Empty ;
InBlock.gif
177
InBlock.gif
178            try
ExpandedSubBlockStart.gifContractedSubBlock.gif
179            dot.gif{
InBlock.gif
180                //根据路径获取节点
InBlock.gif
181                XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode) ;
InBlock.gif
182                if (!(xmlNode==null))
InBlock.gif
183                    strReturn = xmlNode.InnerText ;
ExpandedSubBlockEnd.gif
184            }

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

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

ExpandedSubBlockEnd.gif
191        #endregion

InBlock.gif
192
ContractedSubBlock.gifExpandedSubBlockStart.gif
193        设置节点值设置节点值#region 设置节点值
ExpandedSubBlockStart.gifContractedSubBlock.gif
194        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif195        /**//// 功能:
ExpandedSubBlockStart.gifContractedSubBlock.gif196        /**//// 设置节点值        
ExpandedSubBlockStart.gifContractedSubBlock.gif197        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif198        /**//// <param name="strNode">节点的名称</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif199        /**//// <param name="newValue">节点值</param>
InBlock.gif200        public void SetXmlNodeValue(string xmlNodePath,string xmlNodeValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif
201        dot.gif{
InBlock.gif
202            try
ExpandedSubBlockStart.gifContractedSubBlock.gif
203            dot.gif{
InBlock.gif
204                //可以批量为符合条件的节点进行付值
InBlock.gif
205                XmlNodeList xmlNode=this.xmlDoc.SelectNodes(xmlNodePath);
InBlock.gif
206                if (!(xmlNode==null))
ExpandedSubBlockStart.gifContractedSubBlock.gif
207                dot.gif{
InBlock.gif
208                    foreach(XmlNode xn in xmlNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif
209                    dot.gif{
InBlock.gif
210                        xn.InnerText = xmlNodeValue ;    
ExpandedSubBlockEnd.gif
211                    }

ExpandedSubBlockEnd.gif
212                }

ExpandedSubBlockStart.gifContractedSubBlock.gif
213                /**//**//**//*
InBlock.gif214                 * 根据指定路径获取节点
InBlock.gif215                XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;            
InBlock.gif216                //设置节点值
InBlock.gif217                if (!(xmlNode==null))
ExpandedSubBlockEnd.gif218                    xmlNode.InnerText = xmlNodeValue ;
*/
                
ExpandedSubBlockEnd.gif
219            }

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

ExpandedSubBlockEnd.gif
224        }

ExpandedSubBlockEnd.gif
225        #endregion

InBlock.gif
226
ContractedSubBlock.gifExpandedSubBlockStart.gif
227        设置节点的属性值设置节点的属性值#region 设置节点的属性值
ExpandedSubBlockStart.gifContractedSubBlock.gif
228        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif229        /**//// 功能:
ExpandedSubBlockStart.gifContractedSubBlock.gif230        /**//// 设置节点的属性值    
ExpandedSubBlockStart.gifContractedSubBlock.gif231        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif232        /**//// <param name="xmlNodePath">节点名称</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif233        /**//// <param name="xmlNodeAttribute">属性名称</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif234        /**//// <param name="xmlNodeAttributeValue">属性值</param>
InBlock.gif235        public void SetXmlNodeAttributeValue(string xmlNodePath,string xmlNodeAttribute,string xmlNodeAttributeValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif
236        dot.gif{
InBlock.gif
237            try
ExpandedSubBlockStart.gifContractedSubBlock.gif
238            dot.gif{
InBlock.gif
239                //可以批量为符合条件的节点的属性付值
InBlock.gif
240                XmlNodeList xmlNode=this.xmlDoc.SelectNodes(xmlNodePath);
InBlock.gif
241                if (!(xmlNode==null))
ExpandedSubBlockStart.gifContractedSubBlock.gif
242                dot.gif{
InBlock.gif
243                    foreach(XmlNode xn in xmlNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif
244                    dot.gif{
InBlock.gif
245                        XmlAttributeCollection xmlAttr = xn.Attributes ;
InBlock.gif
246                        for(int i=0 ; i<xmlAttr.Count ; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
247                        dot.gif{
InBlock.gif
248                            if ( xmlAttr.Item(i).Name == xmlNodeAttribute )
ExpandedSubBlockStart.gifContractedSubBlock.gif
249                            dot.gif{
InBlock.gif
250                                xmlAttr.Item(i).Value = xmlNodeAttributeValue;
InBlock.gif
251                                break ;
ExpandedSubBlockEnd.gif
252                            }

ExpandedSubBlockEnd.gif
253                        }
    
ExpandedSubBlockEnd.gif
254                    }

ExpandedSubBlockEnd.gif
255                }

ExpandedSubBlockStart.gifContractedSubBlock.gif
256                /**//**//**//*单个节点
InBlock.gif257                //根据指定路径获取节点
InBlock.gif258                XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;
InBlock.gif259                if (!(xmlNode==null))
InBlock.gif260                {//获取节点的属性,并循环取出需要的属性值
InBlock.gif261                    XmlAttributeCollection xmlAttr = xmlNode.Attributes ;
InBlock.gif262                    for(int i=0 ; i<xmlAttr.Count ; i++)
InBlock.gif263                    {
InBlock.gif264                        if ( xmlAttr.Item(i).Name == xmlNodeAttribute )
InBlock.gif265                        {
InBlock.gif266                            xmlAttr.Item(i).Value = xmlNodeAttributeValue;
InBlock.gif267                            break ;
InBlock.gif268                        }
InBlock.gif269                    }    
InBlock.gif270                }
ExpandedSubBlockEnd.gif271                
*/

ExpandedSubBlockEnd.gif
272            }

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

ExpandedSubBlockEnd.gif
277        }

ExpandedSubBlockEnd.gif
278        #endregion

InBlock.gif
279
ContractedSubBlock.gifExpandedSubBlockStart.gif
280        添加添加#region 添加
ExpandedSubBlockStart.gifContractedSubBlock.gif
281        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif282        /**//// 获取XML文件的根元素
ExpandedSubBlockStart.gifContractedSubBlock.gif283        /**//// </summary>
InBlock.gif284        public XmlNode GetXmlRoot()
ExpandedSubBlockStart.gifContractedSubBlock.gif
285        dot.gif{
InBlock.gif
286            return xmlDoc.DocumentElement ;
ExpandedSubBlockEnd.gif
287        }

InBlock.gif
288
ExpandedSubBlockStart.gifContractedSubBlock.gif
289        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif290        /**//// 在根节点下添加父节点
ExpandedSubBlockStart.gifContractedSubBlock.gif291        /**//// </summary>
InBlock.gif292        public void AddParentNode(string parentNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif
293        dot.gif{
InBlock.gif
294            try
ExpandedSubBlockStart.gifContractedSubBlock.gif
295            dot.gif{
InBlock.gif
296                XmlNode root = GetXmlRoot() ;
InBlock.gif
297                XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode) ;
InBlock.gif
298                root.AppendChild(parentXmlNode) ;                
ExpandedSubBlockEnd.gif
299            }

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

ExpandedSubBlockEnd.gif
304        }

InBlock.gif
305        
ExpandedSubBlockStart.gifContractedSubBlock.gif
306        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif307        /**//// 向一个已经存在的父节点中插入一个子节点
ExpandedSubBlockStart.gifContractedSubBlock.gif308        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif309        /**//// <param name="parentNodePath">父节点</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif310        /**//// <param name="childNodePath">字节点名称</param>
InBlock.gif311        public void AddChildNode( string parentNodePath,string childnodename )
ExpandedSubBlockStart.gifContractedSubBlock.gif
312        dot.gif{
InBlock.gif
313            try
ExpandedSubBlockStart.gifContractedSubBlock.gif
314            dot.gif{
InBlock.gif
315                XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath) ;                
InBlock.gif
316                if(!((parentXmlNode)==null))//如果此节点存在
ExpandedSubBlockStart.gifContractedSubBlock.gif
317                dot.gif{    
InBlock.gif
318                    XmlNode childXmlNode =  xmlDoc.CreateElement(childnodename) ;                
InBlock.gif
319                    parentXmlNode.AppendChild( childXmlNode ) ;    
ExpandedSubBlockEnd.gif
320                }

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

InBlock.gif
324                            
ExpandedSubBlockEnd.gif
325            }

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

ExpandedSubBlockEnd.gif
330        }

InBlock.gif
331        
ExpandedSubBlockStart.gifContractedSubBlock.gif
332        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif333        /**//// 向一个节点添加属性
ExpandedSubBlockStart.gifContractedSubBlock.gif334        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif335        /**//// <param name="NodePath">节点路径</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif336        /**//// <param name="NodeAttribute">属性名</param>
InBlock.gif337        public void AddAttribute( string NodePath , string NodeAttribute)
ExpandedSubBlockStart.gifContractedSubBlock.gif
338        dot.gif{
InBlock.gif
339            privateAddAttribute(NodePath,NodeAttribute,"");
ExpandedSubBlockEnd.gif
340        }

ExpandedSubBlockStart.gifContractedSubBlock.gif
341        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif342        /**//// 
ExpandedSubBlockStart.gifContractedSubBlock.gif343        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif344        /**//// <param name="NodePath"></param>
ExpandedSubBlockStart.gifContractedSubBlock.gif345        /**//// <param name="NodeAttribute"></param>
ExpandedSubBlockStart.gifContractedSubBlock.gif346        /**//// <param name="NodeAttributeValue"></param>
InBlock.gif347        private void privateAddAttribute( string NodePath , string NodeAttribute,string NodeAttributeValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif
348        dot.gif{
InBlock.gif
349            try
ExpandedSubBlockStart.gifContractedSubBlock.gif
350            dot.gif{
InBlock.gif
351                XmlNode nodePath = xmlDoc.SelectSingleNode( NodePath ) ;
InBlock.gif
352                if (!(nodePath==null))
ExpandedSubBlockStart.gifContractedSubBlock.gif
353                dot.gif{    
InBlock.gif
354                    XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute); 
InBlock.gif
355                    nodeAttribute.Value=NodeAttributeValue;
InBlock.gif
356                    nodePath.Attributes.Append(nodeAttribute) ;                        
ExpandedSubBlockEnd.gif
357                }

ExpandedSubBlockEnd.gif
358            }

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

ExpandedSubBlockEnd.gif
363        }

ExpandedSubBlockStart.gifContractedSubBlock.gif
364        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif365        /**////  向一个节点添加属性,并付值
ExpandedSubBlockStart.gifContractedSubBlock.gif366        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif367        /**//// <param name="NodePath">节点</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif368        /**//// <param name="NodeAttribute">属性名</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif369        /**//// <param name="NodeAttributeValue">属性值</param>
InBlock.gif370        public void AddAttribute( string NodePath , string NodeAttribute,string NodeAttributeValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif
371        dot.gif{
InBlock.gif
372            privateAddAttribute(NodePath,NodeAttribute,NodeAttributeValue);
ExpandedSubBlockEnd.gif
373        }

ExpandedSubBlockEnd.gif
374        #endregion

InBlock.gif
375
ContractedSubBlock.gifExpandedSubBlockStart.gif
376        删除删除#region 删除
ExpandedSubBlockStart.gifContractedSubBlock.gif
377        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif378        /**//// 删除节点的一个属性
ExpandedSubBlockStart.gifContractedSubBlock.gif379        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif380        /**//// <param name="NodePath">节点所在的xpath表达式</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif381        /**//// <param name="NodeAttribute">属性名</param>
InBlock.gif382        public void DeleteAttribute( string NodePath , string NodeAttribute)
ExpandedSubBlockStart.gifContractedSubBlock.gif
383        dot.gif{            
InBlock.gif
384            XmlNodeList nodePath =this.xmlDoc.SelectNodes(NodePath);            
InBlock.gif
385            if (!(nodePath==null))
ExpandedSubBlockStart.gifContractedSubBlock.gif
386            dot.gif{
InBlock.gif
387                foreach (XmlNode tempxn in nodePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif
388                dot.gif{
InBlock.gif
389                    XmlAttributeCollection xmlAttr = tempxn.Attributes ;
InBlock.gif
390                    for(int i=0 ; i<xmlAttr.Count ; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
391                    dot.gif{
InBlock.gif
392                        if ( xmlAttr.Item(i).Name == NodeAttribute)
ExpandedSubBlockStart.gifContractedSubBlock.gif
393                        dot.gif{
InBlock.gif
394                            tempxn.Attributes.RemoveAt(i);
InBlock.gif
395                            break ;
ExpandedSubBlockEnd.gif
396                        }

ExpandedSubBlockEnd.gif
397                    }

ExpandedSubBlockEnd.gif
398                }

ExpandedSubBlockEnd.gif
399            }

ExpandedSubBlockEnd.gif
400        }

InBlock.gif
401        
ExpandedSubBlockStart.gifContractedSubBlock.gif
402        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif403        /**//// 删除节点,当其属性值等于给定的值时
ExpandedSubBlockStart.gifContractedSubBlock.gif404        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif405        /**//// <param name="NodePath">节点所在的xpath表达式</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif406        /**//// <param name="NodeAttribute">属性</param>
ExpandedSubBlockStart.gifContractedSubBlock.gif407        /**//// <param name="NodeAttributeValue"></param>
InBlock.gif408        public void DeleteAttribute( string NodePath , string NodeAttribute , string NodeAttributeValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif
409        dot.gif{
InBlock.gif
410            XmlNodeList nodePath =this.xmlDoc.SelectNodes(NodePath);            
InBlock.gif
411            if (!(nodePath==null))
ExpandedSubBlockStart.gifContractedSubBlock.gif
412            dot.gif{
InBlock.gif
413                foreach (XmlNode tempxn in nodePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif
414                dot.gif{
InBlock.gif
415                    XmlAttributeCollection xmlAttr = tempxn.Attributes ;
InBlock.gif
416                    for(int i=0 ; i<xmlAttr.Count ; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
417                    dot.gif{
InBlock.gif
418                        if ( xmlAttr.Item(i).Name == NodeAttribute && xmlAttr.Item(i).Value==NodeAttributeValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif
419                        dot.gif{
InBlock.gif
420                            tempxn.Attributes.RemoveAt(i);
InBlock.gif
421                            break ;
ExpandedSubBlockEnd.gif
422                        }

ExpandedSubBlockEnd.gif
423                    }

ExpandedSubBlockEnd.gif
424                }

ExpandedSubBlockEnd.gif
425            }

ExpandedSubBlockEnd.gif
426        }

ExpandedSubBlockStart.gifContractedSubBlock.gif
427        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif428        /**//// 删除节点
ExpandedSubBlockStart.gifContractedSubBlock.gif429        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif430        /**//// <param name="tempXmlNode"></param>
ExpandedSubBlockStart.gifContractedSubBlock.gif431        /**//// <remarks></remarks>
ExpandedSubBlockStart.gifContractedSubBlock.gif432        public void DeleteXmlNode(string tempXmlNode)dot.gif{    
InBlock.gif
433            XmlNodeList nodePath =this.xmlDoc.SelectNodes(tempXmlNode);
InBlock.gif
434            if (!(nodePath==null))
ExpandedSubBlockStart.gifContractedSubBlock.gif
435            dot.gif{
InBlock.gif
436                foreach(XmlNode xn in nodePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif
437                dot.gif{
InBlock.gif
438                    xn.ParentNode.RemoveChild(xn);        
ExpandedSubBlockEnd.gif
439                }

ExpandedSubBlockEnd.gif
440            }

ExpandedSubBlockEnd.gif
441        }

InBlock.gif
442
ExpandedSubBlockEnd.gif
443        #endregion

InBlock.gif
444
ContractedSubBlock.gifExpandedSubBlockStart.gif
445        XML文档事件XML文档事件#region XML文档事件
ExpandedSubBlockStart.gifContractedSubBlock.gif
446        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif447        /**//// 
ExpandedSubBlockStart.gifContractedSubBlock.gif448        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif449        /**//// <param name="src"></param>
ExpandedSubBlockStart.gifContractedSubBlock.gif450        /**//// <param name="args"></param>
InBlock.gif451        private  void nodeInsertEvent(Object src, XmlNodeChangedEventArgs args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
452        dot.gif{
InBlock.gif
453            //保存设置
InBlock.gif
454            SaveXmlDocument();
ExpandedSubBlockEnd.gif
455        }

ExpandedSubBlockStart.gifContractedSubBlock.gif
456        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif457        /**//// 
ExpandedSubBlockStart.gifContractedSubBlock.gif458        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif459        /**//// <param name="src"></param>
ExpandedSubBlockStart.gifContractedSubBlock.gif460        /**//// <param name="args"></param>
InBlock.gif461        private  void nodeDeleteEvent(Object src, XmlNodeChangedEventArgs args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
462        dot.gif{
InBlock.gif
463            //保存设置
InBlock.gif
464            SaveXmlDocument();
ExpandedSubBlockEnd.gif
465        }

ExpandedSubBlockStart.gifContractedSubBlock.gif
466        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif467        /**//// 
ExpandedSubBlockStart.gifContractedSubBlock.gif468        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif469        /**//// <param name="src"></param>
ExpandedSubBlockStart.gifContractedSubBlock.gif470        /**//// <param name="args"></param>
InBlock.gif471        private  void nodeUpdateEvent(Object src, XmlNodeChangedEventArgs args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
472        dot.gif{
InBlock.gif
473            //保存设置
InBlock.gif
474            SaveXmlDocument();
ExpandedSubBlockEnd.gif
475        }

ExpandedSubBlockEnd.gif
476        #endregion

InBlock.gif
477
ContractedSubBlock.gifExpandedSubBlockStart.gif
478        保存XML文件保存XML文件#region 保存XML文件
ExpandedSubBlockStart.gifContractedSubBlock.gif
479        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif480        /**//// 功能: 
ExpandedSubBlockStart.gifContractedSubBlock.gif481        /**//// 保存XML文件
ExpandedSubBlockStart.gifContractedSubBlock.gif482        /**//// 
ExpandedSubBlockStart.gifContractedSubBlock.gif483        /**//// </summary>
InBlock.gif484        public void SaveXmlDocument()
ExpandedSubBlockStart.gifContractedSubBlock.gif
485        dot.gif{
InBlock.gif
486            try
ExpandedSubBlockStart.gifContractedSubBlock.gif
487            dot.gif{
InBlock.gif
488                //保存设置的结果
InBlock.gif
489                ifthis.xmlFilePathType == enumXmlPathType.AbsolutePath )
ExpandedSubBlockStart.gifContractedSubBlock.gif
490                dot.gif{
InBlock.gif
491                    Savexml( xmlFilePath ) ;
ExpandedSubBlockEnd.gif
492                }

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

ExpandedSubBlockEnd.gif
497            }

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

ExpandedSubBlockEnd.gif
502        }

InBlock.gif
503    
ExpandedSubBlockStart.gifContractedSubBlock.gif
504        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif505        /**//// 功能: 
ExpandedSubBlockStart.gifContractedSubBlock.gif506        /**//// 保存XML文件    
ExpandedSubBlockStart.gifContractedSubBlock.gif507        /**//// </summary>
InBlock.gif508        public void SaveXmlDocument(string tempXMLFilePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif
509        dot.gif{
InBlock.gif
510            try
ExpandedSubBlockStart.gifContractedSubBlock.gif
511            dot.gif{
InBlock.gif
512                //保存设置的结果
InBlock.gif
513                Savexml(tempXMLFilePath);
ExpandedSubBlockEnd.gif
514            }

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

ExpandedSubBlockEnd.gif
519        }

ExpandedSubBlockStart.gifContractedSubBlock.gif
520        /**//**//**//// <summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif521        /**//// 
ExpandedSubBlockStart.gifContractedSubBlock.gif522        /**//// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif523        /**//// <param name="filepath"></param>
InBlock.gif524        private void Savexml(string filepath)
ExpandedSubBlockStart.gifContractedSubBlock.gif
525        dot.gif{
InBlock.gif
526            xmlDoc.Save(filepath);
ExpandedSubBlockEnd.gif
527        }

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

InBlock.gif
532
ExpandedBlockEnd.gif
533}

None.gif
534
None.gif
535
None.gif

转: http://www.knowsky.com/340508.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值