好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

读写xml所有节点个人小结 和 读取xml节点的数据总结

读: 
     // 打开某文件(假设web.config在根目录中)
     string  filename = Server.MapPath( " / " )  +   @" WebApplication1\web.config " ;
    XmlDocument xmldoc =   new  XmlDocument();
    xmldoc.Load(filename);
 
     // 得到顶层节点列表
    XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;
     foreach (XmlElement element  in  topM)
     {
      if (element.Name.ToLower() == " appsettings " )
      {
 
       // 得到该节点的子节点
      XmlNodeList nodelist = element.ChildNodes;
 
       if  ( nodelist.Count  > 0  )
       {
        // DropDownList1.Items.Clear();
 
        foreach (XmlElement el  in  nodelist) // 读元素值
        {
         // DropDownList1.Items.Add(el.Attributes["key"].InnerXml);
         // this.TextBox2.Text=el.Attributes["key"].InnerText;
         this .TextBox2.Text = el.Attributes[ " key " ].Value;
         this .Label1.Text = el.Attributes[ " value " ].Value;
 
 
             // 同样在这里可以修改元素值,在后面save。
          //   el.Attributes["value"].Value=this.TextBox2.Text;
       }
 

      }

     }

    }

    xmldoc.Save(filename);
 
在某节点下增加一个元素,并设置值:
 
     if (element.Name.ToLower() == " appsettings " )
     {
 
     XmlElement elem  = xmldoc.CreateElement( " add " );
     
     element.AppendChild(elem);
     elem.InnerText = " ltp " ;
 
     xmldoc.Save(filename);
       
    }
 
效果:
查看更多关于读写xml所有节点个人小结 和 读取xml节点的数据总结的详细内容...

  阅读:42次