C# 對XML基本操作代碼總結(jié)
更新時(shí)間:2011年10月10日 19:38:05 作者:
C# 對XML基本操作包括讀取節(jié)點(diǎn)的數(shù)據(jù),添加節(jié)點(diǎn)。讀取節(jié)點(diǎn)屬性,修改節(jié)點(diǎn)屬性等
具體如下:
XML文件:文件在MyDocument文件夾下
<?xml version="1.0" encoding="utf-8"?>
<PersonF xmlns="" Name="(test)work hard work smart!">
<person Name="Person1">
<ID>1</ID>
<Name>XiaoA</Name>
<Age>59</Age>
</person>
<person Name="Person2">
<ID>2</ID>
<Name>XiaoB</Name>
<Age>29</Age>
</person>
<person Name="Person3">
<ID>3</ID>
<Name>XiaoC</Name>
<Age>103</Age>
</person>
<person Name="Person4">
<ID>4</ID>
<Name>XiaoD</Name>
<Age>59</Age>
</person>
</PersonF>
Code:說明都在注釋里。
void TestXML()
{
XmlDocument doc = new XmlDocument();
string path = "http://www.dhdzp.com/MyDocument/Person.xml";
try
{
doc.Load(path);
//1、讀取單個(gè)節(jié)點(diǎn)的數(shù)據(jù)
XmlNode node = doc.SelectSingleNode("PersonF");
//2、讀取多個(gè)節(jié)點(diǎn)的數(shù)據(jù)
XmlNodeList nodeList1 = doc.SelectNodes("PersonF/person");
//3.1 讀取具體節(jié)點(diǎn)的具體值 如:屬性為Person2的第二個(gè)節(jié)點(diǎn)Name的InnerText
XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName("person");
foreach (XmlNode node2 in nodeList1) //當(dāng)然也能用nodeList的值
{
if (node2.Attributes["Name"].InnerText == "Person2")
{
Console.WriteLine(node2.ChildNodes[1].InnerText);
}
}
//3.2 讀取ID為2所在的節(jié)點(diǎn)第二個(gè)子節(jié)點(diǎn)Name的InnerText
XmlNode node3 = doc.SelectSingleNode("PersonF/person[ID=2]");
string strNode3 = node3.ChildNodes[1].InnerText;
//3.3利用下面的方法可以找到ID為2的節(jié)點(diǎn)
XmlNodeList nodeList2 = doc.SelectNodes("http://person//ID");
XmlNode node4 = null;
foreach (XmlNode node5 in nodeList2)
{
if (node5.InnerText == "2")
{
node4 = node5;
break;
}
}
Console.WriteLine(node4.InnerText);
//4、讀取節(jié)點(diǎn)的屬性
string Name = node.Attributes["Name"].InnerText;
//5 修改節(jié)點(diǎn)的屬性
node.Attributes["Name"].InnerText = "work hard work smart!";
doc.Save(path);
//6 添加自定義的節(jié)點(diǎn)
XmlTextReader reader = new XmlTextReader(path);
XmlElement root = doc.DocumentElement;//獲取根節(jié)點(diǎn)
XmlElement tagOuter = doc.CreateElement("person");
XmlElement tagIN = doc.CreateElement("Name");
tagIN.InnerText = "work hard work smart!";
tagOuter.AppendChild(tagIN);
root.AppendChild(tagOuter);//添加tagOuter到XML文件的最后
reader.Close();
doc.Save(path);
}
catch (System.Exception e)
{
throw new Exception(e.Message);
}
}
XML文件:文件在MyDocument文件夾下
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<PersonF xmlns="" Name="(test)work hard work smart!">
<person Name="Person1">
<ID>1</ID>
<Name>XiaoA</Name>
<Age>59</Age>
</person>
<person Name="Person2">
<ID>2</ID>
<Name>XiaoB</Name>
<Age>29</Age>
</person>
<person Name="Person3">
<ID>3</ID>
<Name>XiaoC</Name>
<Age>103</Age>
</person>
<person Name="Person4">
<ID>4</ID>
<Name>XiaoD</Name>
<Age>59</Age>
</person>
</PersonF>
Code:說明都在注釋里。
復(fù)制代碼 代碼如下:
void TestXML()
{
XmlDocument doc = new XmlDocument();
string path = "http://www.dhdzp.com/MyDocument/Person.xml";
try
{
doc.Load(path);
//1、讀取單個(gè)節(jié)點(diǎn)的數(shù)據(jù)
XmlNode node = doc.SelectSingleNode("PersonF");
//2、讀取多個(gè)節(jié)點(diǎn)的數(shù)據(jù)
XmlNodeList nodeList1 = doc.SelectNodes("PersonF/person");
//3.1 讀取具體節(jié)點(diǎn)的具體值 如:屬性為Person2的第二個(gè)節(jié)點(diǎn)Name的InnerText
XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName("person");
foreach (XmlNode node2 in nodeList1) //當(dāng)然也能用nodeList的值
{
if (node2.Attributes["Name"].InnerText == "Person2")
{
Console.WriteLine(node2.ChildNodes[1].InnerText);
}
}
//3.2 讀取ID為2所在的節(jié)點(diǎn)第二個(gè)子節(jié)點(diǎn)Name的InnerText
XmlNode node3 = doc.SelectSingleNode("PersonF/person[ID=2]");
string strNode3 = node3.ChildNodes[1].InnerText;
//3.3利用下面的方法可以找到ID為2的節(jié)點(diǎn)
XmlNodeList nodeList2 = doc.SelectNodes("http://person//ID");
XmlNode node4 = null;
foreach (XmlNode node5 in nodeList2)
{
if (node5.InnerText == "2")
{
node4 = node5;
break;
}
}
Console.WriteLine(node4.InnerText);
//4、讀取節(jié)點(diǎn)的屬性
string Name = node.Attributes["Name"].InnerText;
//5 修改節(jié)點(diǎn)的屬性
node.Attributes["Name"].InnerText = "work hard work smart!";
doc.Save(path);
//6 添加自定義的節(jié)點(diǎn)
XmlTextReader reader = new XmlTextReader(path);
XmlElement root = doc.DocumentElement;//獲取根節(jié)點(diǎn)
XmlElement tagOuter = doc.CreateElement("person");
XmlElement tagIN = doc.CreateElement("Name");
tagIN.InnerText = "work hard work smart!";
tagOuter.AppendChild(tagIN);
root.AppendChild(tagOuter);//添加tagOuter到XML文件的最后
reader.Close();
doc.Save(path);
}
catch (System.Exception e)
{
throw new Exception(e.Message);
}
}
您可能感興趣的文章:
相關(guān)文章
C#.Net基于正則表達(dá)式抓取百度百家文章列表的方法示例
這篇文章主要介紹了C#.Net基于正則表達(dá)式抓取百度百家文章列表的方法,結(jié)合實(shí)例形式分析了C#獲取百度百家文章內(nèi)容及使用正則表達(dá)式匹配標(biāo)題、內(nèi)容、地址等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
C#使用委托(delegate)實(shí)現(xiàn)在兩個(gè)form之間傳遞數(shù)據(jù)的方法
這篇文章主要介紹了C#使用委托(delegate)實(shí)現(xiàn)在兩個(gè)form之間傳遞數(shù)據(jù)的方法,涉及C#委托的使用技巧,需要的朋友可以參考下2015-04-04
C#Process的OutputDataReceived事件不觸發(fā)問題及解決
這篇文章主要介紹了C#Process的OutputDataReceived事件不觸發(fā)問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
c#基礎(chǔ)系列之System.String的深入理解
這篇文章主要給大家介紹了關(guān)于c#基礎(chǔ)系列之System.String的深入理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
.NET連接MongoDB數(shù)據(jù)庫實(shí)例教程
這則小竅門將講述如何開發(fā)一個(gè).NET應(yīng)用來連接Mongo數(shù)據(jù)庫并執(zhí)行多種操作。同時(shí)還稍微涉及了Mongo數(shù)據(jù)庫和多種命令2013-11-11
C#切換鼠標(biāo)左右鍵習(xí)慣無需控制面板中修改
本人一直喜歡左手使用鼠標(biāo),偶爾同事會臨時(shí)操作一下,因?yàn)樗牧?xí)慣是右手,還得在控制面板里進(jìn)行更改,太麻煩了所以就編寫一個(gè)控制臺程序,雙擊一下即可切換左右鍵,熱愛懶人的你可不要錯(cuò)過了哈2013-02-02

