C#讀取xml節點數據辦法小結。本站提示廣大學習愛好者:(C#讀取xml節點數據辦法小結)文章只能為提供參考,不一定能成為您想要的結果。以下是C#讀取xml節點數據辦法小結正文
本文實例總結了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#法式設計有所贊助。