栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

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

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

读: 
    //打开某文件(假设web.config在根目录中)
    string filename=Server.MapPath("/") + @"WebApplication1web.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);
       
    }
 
效果:
  
    
    ltp
  

 
在某节点下增加一个元素,并增加两个属性:
    if(element.Name.ToLower()=="appsettings")
    {
 
     XmlElement elem =xmldoc.CreateElement("add");
     element.AppendChild(elem);
 
     XmlAttribute xa=xmldoc.CreateAttribute("key");
     xa.Value="ltp";
 
     XmlAttribute xa2=xmldoc.CreateAttribute("value");
     xa2.Value="first";
 
     elem.SetAttributeNode(xa);
     elem.SetAttributeNode(xa2);
 

     xmldoc.Save(filename);
       
    }
 
效果:
  
    
    
  

 
//添加空元素:
   XmlNode node=doc.CreateElement(groupname);
     node.InnerText="";
     doc.LastChild.AppendChild(node);
 
     doc.Save(xmlfile);
 
删除一个节点元素
   string itemname=this.listBox1.SelectedItem.ToString();
   
   this.listBox1.Items.Remove(this.listBox1.SelectedItem);
 
   //begin del xmlfile
   Xmldocument doc=new Xmldocument();
   doc.Load(xmlfile);
   
   XmlNodeList topM=doc.documentElement.ChildNodes;
   foreach(XmlElement element in topM)
   {
    if(element.Name==this.comboBox1.Text)
    {
 
     //得到该节点的子节点
     XmlNodeList nodelist=element.ChildNodes;      
 
     foreach(XmlElement el in nodelist)//读元素值
     {       
      if(el.Attributes["key"].Value==itemname)
      {
       element.RemoveChild(el);
      }
 
     }//循环元素
      
    }//得到组
 
   }//循环组

   doc.Save(xmlfile);  //一定要保存一下,否则不起作用
 
//筛选数据
private void Reader_Xml(string pathFlie)
{
   Xmldocument Xmldoc=new Xmldocument();
   Xmldoc.Load(pathFlie);
   XmlNodeList Record1=Xmldoc.documentElement.SelectNodes(Code[@id='1'])
   int f=0;
   foreach(XmlNode xnode in Record1)
    {
 
    }
}

     something
     something

 

    



DS.ReadXml("your xmlfile name");
Container.DataItem("bb");
Container.DataItem("cc");
DS.ReadXmlSchema("your xmlfile name");
 


    

如果我要找到123然后取到321应该怎么写呢?
 
using System.XML;
XmlDatadocument xmlDoc = new System.Xml.XmlDatadocument();
xmlDoc.Load(@"c:Config.xml");
XmlElement elem = xmlDoc.GetElementById("add");
string str = elem.Attributes["value"].Value
 
 
/*第三种方法:  SelectSingleNode  读取两种格式的xml *---/
--------------------------------------------------------------------


    
       Data Source=yf; user id=ctm_dbo;password=123             
  

--------------------------------------------------------------------------
Xmldocument doc = new Xmldocument();
doc.Load(strXmlName);
 
    XmlNode node=doc.SelectSingleNode("/configuration/appSettings/ConnectionString");
    if(node!=null)
    {
     string k1=node.Value;    //null
     string k2=node.InnerText;//Data Source=yf; user id=ctm_dbo;password=123
     string k3=node.InnerXml;//Data Source=yf; user id=ctm_dbo;password=123
     node=null;
    }
 
********************************************************************


    
                    
  

**--------------------------------------------------------------------**
     XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
    if(node!=null)
    {
     string k=node.Attributes["key"].Value;
     string v=node.Attributes["value"].Value;
     node=null;
    }
*--------------------------------------------------------------------*
    XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
    if(node!=null)
    {
     XmlNodeReader nr=new XmlNodeReader(node);
     nr.MoveToContent();
    //检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。
     nr.MoveToAttribute("value");
     string s=nr.Value;
     node=null;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/59824.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号