讀寫xml所有節(jié)點(diǎn)個(gè)人小結(jié) 和 讀取xml節(jié)點(diǎn)的數(shù)據(jù)總結(jié)
//打開某文件(假設(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;
}相關(guān)文章
asp.net DataSet轉(zhuǎn)換成josn并輸出示例
如何將DataSet轉(zhuǎn)換成josn并輸出,這是很多新手朋友們遇到的問題,下面有個(gè)不錯(cuò)的示例,希望對(duì)大家有所幫助2014-01-01
詳解可跨域的單點(diǎn)登錄(SSO)實(shí)現(xiàn)方案【附.net代碼】
本篇文章主要介紹了可跨域的單點(diǎn)登錄(SSO)實(shí)現(xiàn)方案,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
ASP.NET?Core中使用Redis實(shí)現(xiàn)緩存
本文詳細(xì)講解了ASP.NET?Core中使用Redis實(shí)現(xiàn)緩存的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
asp .net實(shí)現(xiàn)給圖片添加圖片水印方法示例
圖片上加水印相信每位程序員都會(huì)遇到這個(gè)需求,下面這篇文章主要給大家介紹了asp .net實(shí)現(xiàn)給圖片添加圖片水印的方法,文中給出了完整的實(shí)例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03
asp.net中關(guān)于dropdwonlist無法獲得值問題
用dropdwonlist綁定了一個(gè)數(shù)據(jù)源做選擇,但是當(dāng)提交時(shí),用控件屬性無法獲得相應(yīng)的值,打印出來每次都是顯示的第一個(gè)值2011-11-11
MVC+EasyUI+三層新聞網(wǎng)站建立 主頁布局的方法(五)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第五篇,教大家如何進(jìn)行主頁布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
.net搜索查詢并實(shí)現(xiàn)分頁實(shí)例
.net搜索查詢并實(shí)現(xiàn)分頁實(shí)例,需要的朋友可以參考一下2013-03-03
利用Timer在ASP.NET中實(shí)現(xiàn)計(jì)劃任務(wù)的方法
利用Timer在ASP.NET中實(shí)現(xiàn)計(jì)劃任務(wù)的方法...2007-04-04
asp.net編程獲取項(xiàng)目根目錄實(shí)現(xiàn)方法集合
這篇文章主要介紹了asp.net編程獲取項(xiàng)目根目錄實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析總結(jié)了asp.net針對(duì)項(xiàng)目目錄的操作技巧與注意事項(xiàng),需要的朋友可以參考下2015-11-11

