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

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

 

[NextPage
256                /**//*单个节点
257                //根据指定路径获取节点
258                XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;
259                if (!(xmlNode==null))
260                {//获取节点的属性,并循环取出需要的属性值
261                    XmlAttributeCollection xmlAttr = xmlNode.Attributes ;
262                    for(int i=0 ; i<xmlAttr.Count ; i++)
263                    {
264                        if ( xmlAttr.Item(i).Name == xmlNodeAttribute )
265                        {
266                            xmlAttr.Item(i).Value = xmlNodeAttributeValue;
267                            break ;
268                        }
269                    }   
270                }
271                */
272            }
273            catch(XmlException xmle)
274            {
275                throw xmle ;
276            }
277        }
278        #endregion
279
280        添加#region 添加
281        /** <summary>
282        /// 获取XML文件的根元素
283        /// </summary>
284        public XmlNode GetXmlRoot()
285        {
286            return xmlDoc.DocumentElement ;
287        }
288
289        /** <summary>
290        /// 在根节点下添加父节点
291        /// </summary>
292        public void AddParentNode(string parentNode)
293        {
294            try
295            {
296                XmlNode root = GetXmlRoot() ;
297                XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode) ;
298                root.AppendChild(parentXmlNode) ;               
299            }
300            catch(XmlException xmle)
301            {
302                throw xmle ;
303            }
304        }
305       
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                           

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值