C#中XmlTextWriter讀寫xml文件詳細(xì)介紹
XmlTextWriter類允許你將XML寫到一個(gè)文件中去。這個(gè)類包含了很多方法和屬性,使用這些屬性和方法可以使你更容易地處理XML。為了使用這個(gè)類,你必須首先創(chuàng)建一個(gè)新的XmlTextWriter對(duì)象,然后你可以將XML片斷加入到這個(gè)對(duì)象中。這個(gè)類中包含了不少的方法用于將各種類型的XML元素添加到XML文件中,下表給出了這些方法的名字和描述情況:
方法
描述
WriteStartDocument
書寫版本為“1.0”的 XML 聲明
WriteEndDocument
關(guān)閉任何打開的元素或?qū)傩?br>
Close
關(guān)閉流
WriteDocType
寫出具有指定名稱和可選屬性的 DOCTYPE 聲明
WriteStartElement
寫出指定的開始標(biāo)記
WriteEndElement
關(guān)閉一個(gè)元素
WriteFullEndElement
關(guān)閉一個(gè)元素,并且總是寫入完整的結(jié)束標(biāo)記
WriteElementString
寫出包含字符串值的元素
WriteStartAttribute
書寫屬性的起始內(nèi)容
WriteEndAttribute
關(guān)閉上一個(gè) WriteStartAttribute 調(diào)用
WriteRaw
手動(dòng)書寫原始標(biāo)記
WriteString
書寫一個(gè)字符串
WriteAttributeString
出具有指定值的屬性
WriteCData
寫出包含指定文本的 <![CDATA[...]]> 塊
WriteComment
寫出包含指定文本的注釋 <!--...-->
WriteWhiteSpace
寫出給定的空白
WriteProcessingInstruction
寫出在名稱和文本之間帶有空格的處理指令,如下所示:<?name text?>
如果你對(duì)于XML十分熟悉,那么你一定能很好的理解上面的這些方法。下面我們將給出一個(gè)例子,在這個(gè)例子中,我們將先創(chuàng)建一個(gè)文檔,添加一些元素,然后關(guān)閉這個(gè)文檔。添加了元素后你還可以添加子元素,屬性和其他內(nèi)容。下面的代碼就是這樣的一個(gè)例子,它創(chuàng)建了一個(gè)文件名為title的XML文件。
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlTextWriter writer = new XmlTextWriter("titles.xml", null);
//寫入根元素
writer.WriteStartElement("items");
//加入子元素
writer.WriteElementString("title", "Unreal Tournament 2003");
writer.WriteElementString("title", "C&C: Renegade");
writer.WriteElementString("title", "Dr. Seuss's ABC");
//關(guān)閉根元素,并書寫結(jié)束標(biāo)簽
writer.WriteEndElement();
//將XML寫入文件并且關(guān)閉XmlTextWriter
writer.Close();
}
}
如果你編譯并且執(zhí)行上面的代碼,你將創(chuàng)建這個(gè)XML文件,文件中包含如下內(nèi)容:
<items>
<title>Unreal Tournament 2003</title>
<title>C&C: Renegade</title>
<title>Dr. Seuss's ABC</title>
</items>
上面的代碼創(chuàng)建了一個(gè)名為writer的XmlTextWriter對(duì)象。當(dāng)這個(gè)對(duì)象被創(chuàng)建時(shí),它被關(guān)聯(lián)到一個(gè)名為titles.xml的文件。接著,程序創(chuàng)建了一個(gè)叫做items的根屬性,WriteStartElement方法創(chuàng)建了這個(gè)屬性的開始標(biāo)簽。接下來,程序調(diào)用了WriteElementString方法創(chuàng)建了三個(gè)子元素。從上面的代碼你還可以看到,這個(gè)方法使用第一個(gè)參數(shù)(在上面的程序中是title)作為元素的標(biāo)簽;使用第二個(gè)參數(shù)作為元素的值。當(dāng)你添加了所有的元素后,你需要關(guān)閉根元素。這時(shí)你可以調(diào)用WriteEndElement方法關(guān)閉那個(gè)最近被打開的元素;在本例中,這個(gè)最近被打開的元素就是根元素。當(dāng)所有的數(shù)據(jù)都已經(jīng)寫好,根元素也已經(jīng)關(guān)閉時(shí),你可以將信息傳送給你的XmlTextWriter。這意味著這時(shí)候你可以調(diào)用Close方法關(guān)閉它了。
上面的代碼相對(duì)十分的簡(jiǎn)單,下面我們看一個(gè)使用了XmlTextWriter類中更多方法,功能更加完善的例子。
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlTextWriter writer = new XmlTextWriter("myMedia.xml", null);
//使用自動(dòng)縮進(jìn)便于閱讀
writer.Formatting = Formatting.Indented;
//書寫根元素
writer.WriteStartElement("items");
//開始一個(gè)元素
writer.WriteStartElement("item");
//向先前創(chuàng)建的元素中添加一個(gè)屬性
writer.WriteAttributeString("rating", "R");
//添加子元素
writer.WriteElementString("title", "The Matrix");
writer.WriteElementString("format", "DVD");
//關(guān)閉item元素
writer.WriteEndElement(); // 關(guān)閉元素
//在節(jié)點(diǎn)間添加一些空格
writer.WriteWhitespace("\n");
//使用原始字符串書寫第二個(gè)元素
writer.WriteRaw("<item>" +
"<title>BloodWake</title>" +
"<format>XBox</format>" +
"</item>");
//使用格式化的字符串書寫第三個(gè)元素
writer.WriteRaw("\n <item>\n" +
" <title>Unreal Tournament 2003</title>\n" +
" <format>CD</format>\n" +
" </item>\n");
// 關(guān)閉根元素
writer.WriteFullEndElement();
//將XML寫入文件并關(guān)閉writer
writer.Close();
}
}
上面代碼編譯運(yùn)行后將得到myMedia.xml文件,文件的內(nèi)容為:
<items>
<item rating="R">
<title>The Matrix</title>
<format>DVD</format>
</item>
<item>
<title>BloodWake</title>
<format>XBox</format>
</item>
<item>
<title>Unreal Tournament 2003</title>
<format>CD</format>
</item>
</items>
上面代碼中的注釋說明了這個(gè)程序的功能是如何實(shí)現(xiàn)的。需要記住的一件事是:當(dāng)調(diào)用方法開始一個(gè)操作時(shí),你需要在程序的合適的地方調(diào)用方法結(jié)束這個(gè)操作。例如,你調(diào)用了StartElement,你就必須調(diào)用EndElement關(guān)閉元素;當(dāng)然在這兩個(gè)調(diào)用之間你也可以加入一個(gè)子元素。無(wú)論你何時(shí)調(diào)用EndElement方法,它總是關(guān)閉最近使用StartElement方法打開的那個(gè)元素(這和棧的工作方式很相似)。
使用XmlTextWriter十分的容易,不過我還是建議你自己動(dòng)手試試這些代碼和方法。你試過以后會(huì)發(fā)現(xiàn)這些代碼能夠很容易地集成到你的程序中。你還應(yīng)該記住,XmlTextWriter僅僅是.NET提供的眾多XML類中的一個(gè)。和XmlTextWriter一樣,其他的XML類也十分的容易使用
2)
我用的是一種很笨的方法,但可以幫助初學(xué)者了解訪問XML節(jié)點(diǎn)的過程。
已知有一個(gè)XML文件(bookstore.xml)如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>
1、往<bookstore>節(jié)點(diǎn)中插入一個(gè)<book>節(jié)點(diǎn):
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("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屬性
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS從入門到精通";//設(shè)置文本節(jié)點(diǎn)
xe1.AppendChild(xesub1);//添加到<book>節(jié)點(diǎn)中
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<bookstore>節(jié)點(diǎn)中
xmlDoc.Save("bookstore.xml");
結(jié)果為:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="李贊紅" ISBN="2-3631-4">
<title>CS從入門到精通</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>的文本修改為“亞勝”。
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//獲取bookstore節(jié)點(diǎn)的所有子節(jié)點(diǎn)
foreach(XmlNode xn in nodeList)//遍歷所有子節(jié)點(diǎn)
{
XmlElement xe=(XmlElement)xn;//將子節(jié)點(diǎn)類型轉(zhuǎn)換為XmlElement類型
if(xe.GetAttribute("genre")=="李贊紅")//如果genre屬性值為“李贊紅”
{
xe.SetAttribute("genre","update李贊紅");//則修改該屬性為“update李贊紅”
XmlNodeList nls=xe.ChildNodes;//繼續(xù)獲取xe子節(jié)點(diǎn)的所有子節(jié)點(diǎn)
foreach(XmlNode xn1 in nls)//遍歷
{
XmlElement xe2=(XmlElement)xn1;//轉(zhuǎn)換類型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亞勝";//則修改
break;//找到退出來就可以了
}
}
break;
}
}
xmlDoc.Save("bookstore.xml");//保存。
最后結(jié)果為:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="update李贊紅" ISBN="2-3631-4">
<title>CS從入門到精通</title>
<author>亞勝</author>
<price>58.3</price>
</book>
</bookstore>
3、刪除 <book genre="fantasy" ISBN="2-3631-4">節(jié)點(diǎn)的genre屬性,刪除 <book genre="update李贊紅" ISBN="2-3631-4">節(jié)點(diǎn)。
XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//刪除genre屬性
}
else if(xe.GetAttribute("genre")=="update李贊紅")
{
xe.RemoveAll();//刪除該節(jié)點(diǎn)的全部?jī)?nèi)容
}
}
xmlDoc.Save("bookstore.xml");
最后結(jié)果為:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>
4、顯示所有數(shù)據(jù)。
XmlNode xn=xmlDoc.SelectSingleNode("bookstore");
XmlNodeList xnl=xn.ChildNodes;
foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//顯示屬性值
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//顯示子節(jié)點(diǎn)點(diǎn)文本
}
}
相關(guān)文章
Windows系統(tǒng)中使用C#編寫藍(lán)牙通信程序的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Windows系統(tǒng)中使用C#編寫藍(lán)牙通信程序的簡(jiǎn)單實(shí)例,文中的例子使用到了32feet.NET中的InTheHand.Net.Personal類庫(kù),需要的朋友可以參考下2016-04-04C#實(shí)現(xiàn)的簡(jiǎn)單鏈表類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的簡(jiǎn)單鏈表類,涉及C#針對(duì)鏈表的定義、實(shí)現(xiàn)及鏈表節(jié)點(diǎn)的增加、刪除與修改技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#微信公眾平臺(tái)開發(fā)之a(chǎn)ccess_token的獲取存儲(chǔ)與更新
這篇文章主要介紹了C#微信公眾平臺(tái)開發(fā)之a(chǎn)ccess_token的獲取存儲(chǔ)與更新的相關(guān)資料,需要的朋友可以參考下2016-03-03如何在C#9 中使用頂級(jí)程序 (top-level)
這篇文章主要介紹了如何在C#9 中使用頂級(jí)程序 (top-level),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03