C#读取xml节点数据方法小结
本文实例总结了C#读取xml节点数据的方法。分享给大家供大家参考。具体如下:
第一种:
使用XPath
XML的路径我配置在web.config的appSettings节点下
<appSettings> <addkey="orgCodePath"value="../../template/home/orgCode.xml"/> </appSettings>
XML结构如下:
<?xmlversion="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来进行存储:
Hashtableht=newHashtable(); stringorgCodePath=Server.MapPath(ConfigurationSettings.AppSettings["orgCodePath"]); //stringorgCodePath=Server.MapPath("../../template/home/orgCode.xml"); XmlDocumentxmldoc=newXmlDocument(); xmldoc.Load(orgCodePath); //获取节点列表 XmlNodeListtopM=xmldoc.SelectNodes("//Organization"); foreach(XmlElementelementintopM) { stringid=element.GetElementsByTagName("ID")[0].InnerText; stringdomainName=element.GetElementsByTagName("DomainName")[0].InnerText; ht.Add(id,domainName); }
第二种:
遍历式读取XML
//打开某文件(假设web.config在根目录中) stringfilename=Server.MapPath("/")+@"WebApplication1/web.config"; XmlDocumentxmldoc=newXmlDocument(); xmldoc.Load(filename); //得到顶层节点列表 XmlNodeListtopM=xmldoc.DocumentElement.ChildNodes; foreach(XmlElementelementintopM) { if(element.Name.ToLower()=="appsettings") { //得到该节点的子节点 XmlNodeListnodelist=element.ChildNodes; if(nodelist.Count>0) { //DropDownList1.Items.Clear(); foreach(XmlElementelinnodelist)//读元素值 { //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") { XmlElementelem=xmldoc.CreateElement("add"); element.AppendChild(elem); elem.InnerText="ltp"; xmldoc.Save(filename); }
效果:
<appSettings> <addkey="密码"value="admin"/> <add>ltp</add> </appSettings>
在某节点下增加一个元素,并增加两个属性:
if(element.Name.ToLower()=="appsettings") { XmlElementelem=xmldoc.CreateElement("add"); element.AppendChild(elem); XmlAttributexa=xmldoc.CreateAttribute("key"); xa.Value="ltp"; XmlAttributexa2=xmldoc.CreateAttribute("value"); xa2.Value="first"; elem.SetAttributeNode(xa); elem.SetAttributeNode(xa2); xmldoc.Save(filename); }
效果:
<appSettings> <addkey="密码"value="admin"/> <addkey="ltp"value="first"/> </appSettings>
添加空元素:
XmlNodenode=doc.CreateElement(groupname); node.InnerText=""; doc.LastChild.AppendChild(node); doc.Save(xmlfile);
删除一个节点元素:
stringitemname=this.listBox1.SelectedItem.ToString(); this.listBox1.Items.Remove(this.listBox1.SelectedItem); //begindelxmlfile XmlDocumentdoc=newXmlDocument(); doc.Load(xmlfile); XmlNodeListtopM=doc.DocumentElement.ChildNodes; foreach(XmlElementelementintopM) { if(element.Name==this.comboBox1.Text) { //得到该节点的子节点 XmlNodeListnodelist=element.ChildNodes; foreach(XmlElementelinnodelist)//读元素值 { if(el.Attributes["key"].Value==itemname) { element.RemoveChild(el); } }//循环元素 }//得到组 }//循环组 doc.Save(xmlfile);//一定要保存一下,否则不起作用 //筛选数据 privatevoidReader_Xml(stringpathFlie) { XmlDocumentXmldoc=newXmlDocument(); Xmldoc.Load(pathFlie); XmlNodeListRecord1=Xmldoc.DocumentElement.SelectNodes(Code[@id='1']); intf=0; foreach(XmlNodexnodeinRecord1) { } }
读取xml数据 两种xml方式
<aaa> <bb>something</bb> <cc>something</cc> </aaa> <aaa> <addkey="123"value="321"/> </aaa>
第一种方法:
DS.ReadXml("yourxmlfilename"); Container.DataItem("bb"); Container.DataItem("cc"); DS.ReadXmlSchema("yourxmlfilename");
第二种方法:
<aaa> <addkey="123"value="321"/> </aaa>
如果我要找到123然后取到321应该怎么写呢?
usingSystem.XML; XmlDataDocumentxmlDoc=newSystem.Xml.XmlDataDocument(); xmlDoc.Load(@"c:/Config.xml"); XmlElementelem=xmlDoc.GetElementById("add"); stringstr=elem.Attributes["value"].Value
第三种方法: SelectSingleNode 读取两种格式的xml:
<?xmlversion="1.0"encoding="utf-8"?> <configuration> <appSettings> <ConnectionString>DataSource=yf;userid=ctm_dbo;password=123</ConnectionString> </appSettings> </configuration>
XmlDocumentdoc=newXmlDocument(); doc.Load(strXmlName); XmlNodenode=doc.SelectSingleNode("/configuration/appSettings/ConnectionString"); if(node!=null) { stringk1=node.Value;//null stringk2=node.InnerText;//DataSource=yf;userid=ctm_dbo;password=123 stringk3=node.InnerXml;//DataSource=yf;userid=ctm_dbo;password=123 node=null; }
<?xmlversion="1.0"encoding="utf-8"?> <configuration> <appSettings> <addkey="ConnectionString"value="DataSource=yf;userid=ctm_dbo;password=123"/> </appSettings> </configuration>
XmlNodenode=doc.SelectSingleNode("/configuration/appSettings/add"); if(node!=null) { stringk=node.Attributes["key"].Value; stringv=node.Attributes["value"].Value; node=null; } XmlNodenode=doc.SelectSingleNode("/configuration/appSettings/add"); if(node!=null) { XmlNodeReadernr=newXmlNodeReader(node); nr.MoveToContent(); //检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。 nr.MoveToAttribute("value"); strings=nr.Value; node=null; }
希望本文所述对大家的C#程序设计有所帮助。