当前位置: 移动技术网 > IT编程>开发语言>c# > C#读取xml节点数据方法小结

C#读取xml节点数据方法小结

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例总结了c#读取xml节点数据的方法。分享给大家供大家参考。具体如下:

第一种:
使用xpath
xml的路径我配置在web.config 的appsettings节点下

<appsettings>
  <add key="orgcodepath" value="../../template/home/orgcode.xml"/>
</appsettings>

xml结构如下:

<?xml version="1.0" encoding="utf-8" ?> 
<organizations> 
 <organization> 
  <id>1111</id> 
  <domainname>aa</domainname> 
 </organization> 
 <organization> 
  <id>2222</id> 
  <domainname>bb</domainname> 
 </organization> 
</organizations>

在c#中,我使用hashtable来进行存储:

hashtable ht = new hashtable(); 
string orgcodepath = server.mappath(configurationsettings.appsettings["orgcodepath"]);
//string orgcodepath = server.mappath("../../template/home/orgcode.xml");
xmldocument xmldoc = new xmldocument();
xmldoc.load(orgcodepath);
//获取节点列表 
xmlnodelist topm = xmldoc.selectnodes("//organization"); 
foreach (xmlelement element in topm) 
{ 
 string id = element.getelementsbytagname("id")[0].innertext; 
 string domainname = element.getelementsbytagname("domainname")[0].innertext; 
 ht.add(id, domainname); 
}

第二种:

遍历式读取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); 
}

效果:  

<appsettings> 
  <add key="密码" value="admin" /> 
  <add>ltp</add> 
</appsettings>

在某节点下增加一个元素,并增加两个属性:  

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); 
}

效果:  

<appsettings> 
  <add key="密码" value="admin" /> 
  <add key="ltp" value="first" /> 
</appsettings> 

添加空元素:

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) 
  { 
  } 
}

读取xml数据   两种xml方式

<aaa> 
   <bb>something</bb> 
   <cc>something</cc> 
</aaa> 
<aaa> 
  <add key="123" value="321"/> 
</aaa> 

第一种方法:

ds.readxml("your xmlfile name"); 
container.dataitem("bb"); 
container.dataitem("cc"); 
ds.readxmlschema("your xmlfile name"); 

第二种方法:

<aaa>
  <add key="123" value="321"/>
</aaa>

如果我要找到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 :

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
  <appsettings> 
    <connectionstring>data source=yf; user id=ctm_dbo;password=123</connectionstring>        
 </appsettings> 
</configuration> 

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; 
}
<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
  <appsettings> 
    <add key="connectionstring" value="data source=yf; user id=ctm_dbo;password=123" />        
 </appsettings> 
</configuration> 

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; 
}

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网