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

 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 ;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值