xml操作之创建xml节点

Xml是一个存放数据的小型数据库文件,这个应用也很广泛,先把数据添加保存到xml中,然后在读取出来,今天就来看看如何创建xml节点并添加数据,代码如下:

     

   protected   void  InsertXml( string  path)
      {
        DataSet ds 
=   new  Maticsoft.BLL.news().GetList( "  jh_type=148 and jh_status = 1 order by jh_datetime desc " );
        XmlDocument xmldocument 
=   new  XmlDocument();
        xmldocument.Load(path);
        XmlNode node 
=  xmldocument.SelectSingleNode( " adslist " );
        
if (ds != null )
        {
            
foreach (DataRow row  in  ds.Tables[ 0 ].Rows)
            {
                
string  picUrl  =  row[ " jh_pic " ].ToString();
                
string  title  =  row[ " jh_title " ].ToString();
                XmlNode newNode 
=  xmldocument.CreateNode(XmlNodeType.Element,  " item " null );
                newNode.Attributes.Append(CreateNodeAttribute(xmldocument, 
" adurl " , picUrl));
                newNode.Attributes.Append(CreateNodeAttribute(xmldocument, 
" adname " , title));
                newNode.Attributes.Append(CreateNodeAttribute(xmldocument, 
" adlink " " # " ));
                
// 将新创建的节点加入到根目录的节点中
                node.AppendChild(newNode);
            }
        }
        
// 创建新节点
       
        
// 创建属性,字段,并赋值
        xmldocument.Save(path);
    }
    
private   static  XmlAttribute CreateNodeAttribute(XmlDocument doc, String name, String value)
    {
        XmlAttribute attribute 
=  doc.CreateAttribute(name,  null );
        attribute.Value 
=  value;
        
return  attribute;
    }
复制代码

转载于:https://www.cnblogs.com/abenmao/archive/2012/02/28/2372004.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值