C# 對(duì)XML操作入門(mén)實(shí)例
更新時(shí)間:2013年04月09日 10:03:05 作者:
C# 對(duì)XML操作入門(mén)實(shí)例,需要的朋友可以參考一下
已知有一個(gè)XML文件(bookstore.xml)如下:
<?xmlversion="1.0"encoding="gb2312"?>
<bookstore>
<bookgenre="fantasy"ISBN="2-3631-4">
<title>Oberon'sLegacy</title>
<author>Corets,Eva</author>
<price>5.95</price>
</book>
</bookstore>
1、往<bookstore>節(jié)點(diǎn)中插入一個(gè)<book>節(jié)點(diǎn):
XmlDocumentxmlDoc=newXmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNoderoot=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElementxe1=xmlDoc.createElement_x("book");//創(chuàng)建一個(gè)<book>節(jié)點(diǎn)
xe1.SetAttribute("genre","李贊紅");//設(shè)置該節(jié)點(diǎn)genre屬性
xe1.SetAttribute("ISBN","2-3631-4");//設(shè)置該節(jié)點(diǎn)ISBN屬性
XmlElementxesub1=xmlDoc.createElement_x("title");
xesub1.InnerText="CS從入門(mén)到精通";//設(shè)置文本節(jié)點(diǎn)
xe1.AppendChild(xesub1);//添加到<book>節(jié)點(diǎn)中
XmlElementxesub2=xmlDoc.createElement_x("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElementxesub3=xmlDoc.createElement_x("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<bookstore>節(jié)點(diǎn)中
xmlDoc.Save("bookstore.xml");
//================
結(jié)果為:
<?xmlversion="1.0"encoding="gb2312"?>
<bookstore>
<bookgenre="fantasy"ISBN="2-3631-4">
<title>Oberon'sLegacy</title>
<author>Corets,Eva</author>
<price>5.95</price>
</book>
<bookgenre="李贊紅"ISBN="2-3631-4">
<title>CS從入門(mén)到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>
2、修改節(jié)點(diǎn):將genre屬性值為“李贊紅“的節(jié)點(diǎn)的genre值改為“update李贊紅”,將該節(jié)點(diǎn)的子節(jié)點(diǎn)<author>的文本修改為“亞勝”。
XmlNodeListnodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//獲取bookstore節(jié)點(diǎn)的所有子節(jié)點(diǎn)
foreach(XmlNodexninnodeList)//遍歷所有子節(jié)點(diǎn)
{
XmlElementxe=(XmlElement)xn;//將子節(jié)點(diǎn)類(lèi)型轉(zhuǎn)換為XmlElement類(lèi)型
if(xe.GetAttribute("genre")=="李贊紅")//如果genre屬性值為“李贊紅”
{
xe.SetAttribute("genre","update李贊紅");//則修改該屬性為“update李贊紅”
XmlNodeListnls=xe.ChildNodes;//繼續(xù)獲取xe子節(jié)點(diǎn)的所有子節(jié)點(diǎn)
foreach(XmlNodexn1innls)//遍歷
{
XmlElementxe2=(XmlElement)xn1;//轉(zhuǎn)換類(lèi)型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亞勝";//則修改
break;//找到退出來(lái)就可以了
}
}
break;
}
}
xmlDoc.Save("bookstore.xml");//保存。
//=================
最后結(jié)果為:
<?xmlversion="1.0"encoding="gb2312"?>
<bookstore>
<bookgenre="fantasy"ISBN="2-3631-4">
<title>Oberon'sLegacy</title>
<author>Corets,Eva</author>
<price>5.95</price>
</book>
<bookgenre="update李贊紅"ISBN="2-3631-4">
<title>CS從入門(mén)到精通</title>
<author>亞勝</author>
<price>58.3</price>
</book>
</bookstore>
3、刪除<bookgenre="fantasy"ISBN="2-3631-4">節(jié)點(diǎn)的genre屬性,刪除<bookgenre="update李贊紅"ISBN="2-3631-4">節(jié)點(diǎn)。
XmlNodeListxnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNodexninxnl)
{
XmlElementxe=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//刪除genre屬性
}
elseif(xe.GetAttribute("genre")=="update李贊紅")
{
xe.RemoveAll();//刪除該節(jié)點(diǎn)的全部?jī)?nèi)容
}
}
xmlDoc.Save("bookstore.xml");
//====================
最后結(jié)果為:
<?xmlversion="1.0"encoding="gb2312"?>
<bookstore>
<bookISBN="2-3631-4">
<title>Oberon'sLegacy</title>
<author>Corets,Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>
4、顯示所有數(shù)據(jù)。
XmlNodexn=xmlDoc.SelectSingleNode("bookstore");
XmlNodeListxnl=xn.ChildNodes;
foreach(XmlNodexnfinxnl)
{
XmlElementxe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//顯示屬性值
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeListxnf1=xe.ChildNodes;
foreach(XmlNodexn2inxnf1)
{
Console.WriteLine(xn2.InnerText);//顯示子節(jié)點(diǎn)點(diǎn)文本
}
}
復(fù)制代碼 代碼如下:
<?xmlversion="1.0"encoding="gb2312"?>
<bookstore>
<bookgenre="fantasy"ISBN="2-3631-4">
<title>Oberon'sLegacy</title>
<author>Corets,Eva</author>
<price>5.95</price>
</book>
</bookstore>
1、往<bookstore>節(jié)點(diǎn)中插入一個(gè)<book>節(jié)點(diǎn):
復(fù)制代碼 代碼如下:
XmlDocumentxmlDoc=newXmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNoderoot=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElementxe1=xmlDoc.createElement_x("book");//創(chuàng)建一個(gè)<book>節(jié)點(diǎn)
xe1.SetAttribute("genre","李贊紅");//設(shè)置該節(jié)點(diǎn)genre屬性
xe1.SetAttribute("ISBN","2-3631-4");//設(shè)置該節(jié)點(diǎn)ISBN屬性
XmlElementxesub1=xmlDoc.createElement_x("title");
xesub1.InnerText="CS從入門(mén)到精通";//設(shè)置文本節(jié)點(diǎn)
xe1.AppendChild(xesub1);//添加到<book>節(jié)點(diǎn)中
XmlElementxesub2=xmlDoc.createElement_x("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElementxesub3=xmlDoc.createElement_x("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<bookstore>節(jié)點(diǎn)中
xmlDoc.Save("bookstore.xml");
//================
結(jié)果為:
復(fù)制代碼 代碼如下:
<?xmlversion="1.0"encoding="gb2312"?>
<bookstore>
<bookgenre="fantasy"ISBN="2-3631-4">
<title>Oberon'sLegacy</title>
<author>Corets,Eva</author>
<price>5.95</price>
</book>
<bookgenre="李贊紅"ISBN="2-3631-4">
<title>CS從入門(mén)到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>
2、修改節(jié)點(diǎn):將genre屬性值為“李贊紅“的節(jié)點(diǎn)的genre值改為“update李贊紅”,將該節(jié)點(diǎn)的子節(jié)點(diǎn)<author>的文本修改為“亞勝”。
復(fù)制代碼 代碼如下:
XmlNodeListnodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//獲取bookstore節(jié)點(diǎn)的所有子節(jié)點(diǎn)
foreach(XmlNodexninnodeList)//遍歷所有子節(jié)點(diǎn)
{
XmlElementxe=(XmlElement)xn;//將子節(jié)點(diǎn)類(lèi)型轉(zhuǎn)換為XmlElement類(lèi)型
if(xe.GetAttribute("genre")=="李贊紅")//如果genre屬性值為“李贊紅”
{
xe.SetAttribute("genre","update李贊紅");//則修改該屬性為“update李贊紅”
XmlNodeListnls=xe.ChildNodes;//繼續(xù)獲取xe子節(jié)點(diǎn)的所有子節(jié)點(diǎn)
foreach(XmlNodexn1innls)//遍歷
{
XmlElementxe2=(XmlElement)xn1;//轉(zhuǎn)換類(lèi)型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亞勝";//則修改
break;//找到退出來(lái)就可以了
}
}
break;
}
}
xmlDoc.Save("bookstore.xml");//保存。
//=================
最后結(jié)果為:
復(fù)制代碼 代碼如下:
<?xmlversion="1.0"encoding="gb2312"?>
<bookstore>
<bookgenre="fantasy"ISBN="2-3631-4">
<title>Oberon'sLegacy</title>
<author>Corets,Eva</author>
<price>5.95</price>
</book>
<bookgenre="update李贊紅"ISBN="2-3631-4">
<title>CS從入門(mén)到精通</title>
<author>亞勝</author>
<price>58.3</price>
</book>
</bookstore>
3、刪除<bookgenre="fantasy"ISBN="2-3631-4">節(jié)點(diǎn)的genre屬性,刪除<bookgenre="update李贊紅"ISBN="2-3631-4">節(jié)點(diǎn)。
復(fù)制代碼 代碼如下:
XmlNodeListxnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNodexninxnl)
{
XmlElementxe=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//刪除genre屬性
}
elseif(xe.GetAttribute("genre")=="update李贊紅")
{
xe.RemoveAll();//刪除該節(jié)點(diǎn)的全部?jī)?nèi)容
}
}
xmlDoc.Save("bookstore.xml");
//====================
最后結(jié)果為:
復(fù)制代碼 代碼如下:
<?xmlversion="1.0"encoding="gb2312"?>
<bookstore>
<bookISBN="2-3631-4">
<title>Oberon'sLegacy</title>
<author>Corets,Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>
4、顯示所有數(shù)據(jù)。
復(fù)制代碼 代碼如下:
XmlNodexn=xmlDoc.SelectSingleNode("bookstore");
XmlNodeListxnl=xn.ChildNodes;
foreach(XmlNodexnfinxnl)
{
XmlElementxe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//顯示屬性值
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeListxnf1=xe.ChildNodes;
foreach(XmlNodexn2inxnf1)
{
Console.WriteLine(xn2.InnerText);//顯示子節(jié)點(diǎn)點(diǎn)文本
}
}
相關(guān)文章
C#使用NPOI設(shè)置Excel下拉選項(xiàng)
這篇文章主要為大家詳細(xì)介紹了C#使用NPOI設(shè)置Excel下拉選項(xiàng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
C#字符串?dāng)?shù)組轉(zhuǎn)換為整形數(shù)組的方法
這篇文章主要介紹了C#字符串?dāng)?shù)組轉(zhuǎn)換為整形數(shù)組的方法,涉及C#數(shù)組遍歷與轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-06-06
利用C#自定義一個(gè)時(shí)間類(lèi)型YearMonth
.Net6中加入了兩個(gè)新的時(shí)間類(lèi)型:DateOnly和TimeOnly,但DateOnly和TimeOnly都有相應(yīng)的應(yīng)用場(chǎng)景,所以本文就來(lái)自定義一個(gè)時(shí)間類(lèi)型YearMonth,用于解決實(shí)際項(xiàng)目開(kāi)發(fā)中的需求,希望對(duì)大家有所幫助2023-07-07

