日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

读写xml节点的数据总结

發布時間:2025/3/21 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 读写xml节点的数据总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
讀寫xml節點的數據總結

讀:
//打開某文件(假設web.config在根目錄中)
string filename=Server.MapPath("/") + @"WebApplication1web.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;
}

轉載于:https://www.cnblogs.com/pbwf/archive/2007/05/28/762521.html

總結

以上是生活随笔為你收集整理的读写xml节点的数据总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。