.net操纵xml文件类(c#)_asp.net技巧2(转)

306        /**//// <summary>
307        /// 向一个已经存在的父节点中插入一个子节点
308        /// </summary>
309        /// <param name="parentNodePath">父节点</param>
310        /// <param name="childNodePath">字节点名称</param>
311        public void AddChildNode( string parentNodePath,string childnodename )
312        {
313            try
314            {
315                XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath) ;               
316                if(!((parentXmlNode)==null))//如果此节点存在
317                {   
318                    XmlNode childXmlNode =  xmlDoc.CreateElement(childnodename) ;               
319                    parentXmlNode.AppendChild( childXmlNode ) ;   
320                }
321                else{//如果不存在就放父节点添加
322                    //this.GetXmlRoot().AppendChild(childXmlNode);
323                }
324                           
325            }
326            catch(XmlException xmle)
327            {
328                throw xmle;
329            }
330        }
331       
332        /**//// <summary>
333        /// 向一个节点添加属性
334        /// </summary>
335        /// <param name="NodePath">节点路径</param>
336        /// <param name="NodeAttribute">属性名</param>
337        public void AddAttribute( string NodePath , string NodeAttribute)
338        {
339            privateAddAttribute(NodePath,NodeAttribute,"");
340        }
341        /**//// <summary>
342        ///
343        /// </summary>
344        /// <param name="NodePath"></param>
345        /// <param name="NodeAttribute"></param>
346        /// <param name="NodeAttributeValue"></param>
347        private void privateAddAttribute( string NodePath , string NodeAttribute,string NodeAttributeValue)
348        {
349            try
350            {
351                XmlNode nodePath = xmlDoc.SelectSingleNode( NodePath ) ;
352                if (!(nodePath==null))
353                {   
354                    XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);
355                    nodeAttribute.Value=NodeAttributeValue;
356                    nodePath.Attributes.Append(nodeAttribute) ;                       
357                }
358            }
359            catch(XmlException xmle)
360            {
361                throw xmle;
362            }
363        }
364        /**//// <summary>
365        ///  向一个节点添加属性,并付值
366        /// </summary>
367        /// <param name="NodePath">节点</param>
368        /// <param name="NodeAttribute">属性名</param>
369        /// <param name="NodeAttributeValue">属性值</param>
370        public void AddAttribute( string NodePath , string NodeAttribute,string NodeAttributeValue)
371        {
372            privateAddAttribute(NodePath,NodeAttribute,NodeAttributeValue);
373        }
374        #endregion
375
376        删除#region 删除
377        /**//// <summary>
378        /// 删除节点的一个属性
379        /// </summary>
380        /// <param name="NodePath">节点所在的xpath表达式</param>
381        /// <param name="NodeAttribute">属性名</param>
382        public void DeleteAttribute( string NodePath , string NodeAttribute)
383        {           
384            XmlNodeList nodePath =this.xmlDoc.SelectNodes(NodePath);           
385            if (!(nodePath==null))
386            {
387                foreach (XmlNode tempxn in nodePath)
388                {
389                    XmlAttributeCollection xmlAttr = tempxn.Attributes ;
390                    for(int i=0 ; i<xmlAttr.Count ; i++)
391                    {
392                        if ( xmlAttr.Item(i).Name == NodeAttribute)
393                        {
394                            tempxn.Attributes.RemoveAt(i);
395                            break ;
396                        }
397                    }
398                }
399            }
400        }
401       
402        /**//// <summary>
403        /// 删除节点,当其属性值等于给定的值时
404        /// </summary>
405        /// <param name="NodePath">节点所在的xpath表达式</param>
406        /// <param name="NodeAttribute">属性</param>
407        /// <param name="NodeAttributeValue">值</param>
408        public void DeleteAttribute( string NodePath , string NodeAttribute , string NodeAttributeValue)
409        {
410            XmlNodeList nodePath =this.xmlDoc.SelectNodes(NodePath);           
411            if (!(nodePath==null))
412            {
413                foreach (XmlNode tempxn in nodePath)
414                {
415                    XmlAttributeCollection xmlAttr = tempxn.Attributes ;
416                    for(int i=0 ; i<xmlAttr.Count ; i++)
417                    {
418                        if ( xmlAttr.Item(i).Name == NodeAttribute && xmlAttr.Item(i).Value==NodeAttributeValue)
419                        {
420                            tempxn.Attributes.RemoveAt(i);
421                            break ;
422                        }
423                    }
424                }
425            }
426        }
427        /**//// <summary>
428        /// 删除节点
429        /// </summary>
430        /// <param name="tempXmlNode"></param>
431        /// <remarks></remarks>
432        public void DeleteXmlNode(string tempXmlNode){   
433            XmlNodeList nodePath =this.xmlDoc.SelectNodes(tempXmlNode);
434            if (!(nodePath==null))
435            {
436                foreach(XmlNode xn in nodePath)
437                {
438                    xn.ParentNode.RemoveChild(xn);       
439                }
440            }
441        }
442
443        #endregion
444
445        XML文档事件#region XML文档事件
446        /**//// <summary>
447        ///
448        /// </summary>
449        /// <param name="src"></param>
450        /// <param name="args"></param>
451        private  void nodeInsertEvent(Object src, XmlNodeChangedEventArgs args)
452        {
453            //保存设置
454            SaveXmlDocument();
455        }
456        /**//// <summary>
457        ///
458        /// </summary>
459        /// <param name="src"></param>
460        /// <param name="args"></param>
461        private  void nodeDeleteEvent(Object src, XmlNodeChangedEventArgs args)
462        {
463            //保存设置
464            SaveXmlDocument();
465        }
466        /**//// <summary>
467        ///
468        /// </summary>
469        /// <param name="src"></param>
470        /// <param name="args"></param>
471        private  void nodeUpdateEvent(Object src, XmlNodeChangedEventArgs args)
472        {
473            //保存设置
474            SaveXmlDocument();
475        }
476        #endregion
477
478        保存XML文件#region 保存XML文件
479        /**//// <summary>
480        /// 功能:
481        /// 保存XML文件
482        ///
483        /// </summary>
484        public void SaveXmlDocument()
485        {
486            try
487            {
488                //保存设置的结果
489                if( this.xmlFilePathType == enumXmlPathType.AbsolutePath )
490                {
491                    Savexml( xmlFilePath ) ;
492                }
493                else if( this.xmlFilePathType == enumXmlPathType.VirtualPath )
494                {
495                    Savexml(HttpContext.Current.Server.MapPath(xmlFilePath)) ;
496                }
497            }
498            catch(XmlException xmle)
499            {
500                throw xmle;
501            }
502        }
503   
504        /**//// <summary>
505        /// 功能:
506        /// 保存XML文件   
507        /// </summary>
508        public void SaveXmlDocument(string tempXMLFilePath)
509        {
510            try
511            {
512                //保存设置的结果
513                Savexml(tempXMLFilePath);
514            }
515            catch(XmlException xmle)
516            {
517                throw xmle;
518            }
519        }
520        /**//// <summary>
521        ///
522        /// </summary>
523        /// <param name="filepath"></param>
524        private void Savexml(string filepath)
525        {
526            xmlDoc.Save(filepath);
527        }
528
529        #endregion
530
531    }
532
533}

posted @ 2012-04-20 23:34  sidihu  阅读(154)  评论(0编辑  收藏  举报