C#讀取xml節(jié)點(diǎn)數(shù)據(jù)方法小結(jié)
本文實(shí)例總結(jié)了C#讀取xml節(jié)點(diǎn)數(shù)據(jù)的方法。分享給大家供大家參考。具體如下:
第一種:
使用XPath
XML的路徑我配置在web.config 的appSettings節(jié)點(diǎn)下
<appSettings> <add key="orgCodePath" value="../../template/home/orgCode.xml"/> </appSettings>
XML結(jié)構(gòu)如下:
<?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來(lái)進(jìn)行存儲(chǔ):
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); //獲取節(jié)點(diǎn)列表 XmlNodeList topM = xmldoc.SelectNodes("http://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
//打開(kāi)某文件(假設(shè)web.config在根目錄中) string filename=Server.MapPath("/") + @"WebApplication1/web.config"; XmlDocument xmldoc= new XmlDocument(); xmldoc.Load(filename); //得到頂層節(jié)點(diǎn)列表 XmlNodeList topM=xmldoc.DocumentElement.ChildNodes; foreach(XmlElement element in topM) { if(element.Name.ToLower()=="appsettings") { //得到該節(jié)點(diǎn)的子節(jié)點(diǎn) 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);
在某節(jié)點(diǎn)下增加一個(gè)元素,并設(shè)置值:
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>
在某節(jié)點(diǎn)下增加一個(gè)元素,并增加兩個(gè)屬性:
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);
刪除一個(gè)節(jié)點(diǎn)元素:
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) { //得到該節(jié)點(diǎn)的子節(jié)點(diǎn) XmlNodeList nodelist=element.ChildNodes; foreach(XmlElement el in nodelist)//讀元素值 { if(el.Attributes["key"].Value==itemname) { element.RemoveChild(el); } }//循環(huán)元素 }//得到組 }//循環(huán)組 doc.Save(xmlfile); //一定要保存一下,否則不起作用 //篩選數(shù)據(jù) 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數(shù)據(jù) 兩種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應(yīng)該怎么寫呢?
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(); //檢查當(dāng)前節(jié)點(diǎn)是否是內(nèi)容節(jié)點(diǎn)。如果此節(jié)點(diǎn)不是內(nèi)容節(jié)點(diǎn),則讀取器向前跳至下一個(gè)內(nèi)容節(jié)點(diǎn)或文件結(jié)尾。 nr.MoveToAttribute("value"); string s=nr.Value; node=null; }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#讀取XML的CDATA節(jié)點(diǎn)內(nèi)容實(shí)例詳解
- C#實(shí)現(xiàn)XML文件讀取
- c#讀取XML多級(jí)子節(jié)點(diǎn)
- C# 創(chuàng)建,讀取,寫入XML文件
- C#讀取XML的三種實(shí)現(xiàn)方式
- 詳解c#讀取XML的實(shí)例代碼
- C#中如何使用 XmlReader 讀取XML文件
- C# winfrom實(shí)現(xiàn)讀取修改xml
- 詳解C#借助.NET框架中的XmlTextReader類讀取XML的方法
- C#保存與讀取DataTable信息到XML格式的方法
- C#從文件流讀取xml文件到DataSet并顯示的方法
- C#讀取Excel并轉(zhuǎn)化成XML的方法
- c# 讀取XML文件的示例
相關(guān)文章
C#中使用Interlocked進(jìn)行原子操作的技巧
使用.NET提供的Interlocked類可以對(duì)一些數(shù)據(jù)進(jìn)行原子操作,看起來(lái)似乎跟lock鎖一樣,但它并不是lock鎖,它的原子操作是基于CPU本身的,非阻塞的,所以要比lock的效率高2016-12-12Unity Shader實(shí)現(xiàn)動(dòng)態(tài)霧效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)動(dòng)態(tài)霧效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#中前臺(tái)線程和后臺(tái)線程的區(qū)別與聯(lián)系
這篇文章主要介紹了C#中前臺(tái)線程和后臺(tái)線程的區(qū)別與聯(lián)系,本文先講解了它們的區(qū)別,然后給出了一個(gè)例子來(lái)驗(yàn)證這些區(qū)別,需要的朋友可以參考下2015-06-06C#語(yǔ)言async?await之迭代器工作原理示例解析
這篇文章主要為大家介紹了C#語(yǔ)言async?await之迭代器工作原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05C#中使用Cache框架快速實(shí)現(xiàn)Cache操作
這篇文章介紹了C#中使用Cache框架快速實(shí)現(xiàn)Cache操作的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06