zoukankan      html  css  js  c++  java
  • 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;
        }

    这里面是从数据库里读取了记录添加到了xml文件中,其实很简单的....

    先加载xml 然后创建节点  添加属性 值 

    然后在加载保存xml文档就ok了

    多思考,多创新,才是正道!
  • 相关阅读:
    #特征方程,dp,快速幂#洛谷 4451 [国家集训队]整数的lqp拆分
    #状压dp,贪心#CF1316E Team Building
    #线段树,欧拉函数#CF1114F Please, another Queries on Array?
    #启发式合并,链表#洛谷 3201 [HNOI2009] 梦幻布丁
    #树状数组#洛谷 4113 [HEOI2012]采花
    #链表#洛谷 3794 签到题IV
    #矩阵乘法,斐波那契#洛谷 2544 [AHOI2004] 数字迷阵
    #dp#洛谷 4399 [JSOI2008]Blue Mary的职员分配
    #同余最短路#洛谷 3403 跳楼机
    #网络流,分层图#洛谷 4400 [JSOI2008] Blue Mary的旅行
  • 原文地址:https://www.cnblogs.com/shuang121/p/2093640.html
Copyright © 2011-2022 走看看