C#中使用XmlDocument類來創(chuàng)建和修改XML格式的數(shù)據(jù)文件
通過XmlDocument類修改XML文檔數(shù)據(jù),通常需要以下幾個(gè)主要步驟或其中幾個(gè)步驟。
(1)獲取一個(gè)包含XML文檔數(shù)據(jù)的XmlDocument類對(duì)象,通常有兩種方法來實(shí)現(xiàn)這個(gè)功能:
通過XmlDocument類的構(gòu)造函數(shù)創(chuàng)建不包含任何結(jié)點(diǎn)的空對(duì)象,常用默認(rèn)構(gòu)造函數(shù)。
(2)通過XmlDocument類的ChildNodes和Item屬性獲取某個(gè)結(jié)點(diǎn)(XmlNode類型),通過XmlNode的Name、Value、InnerText等屬性修改選中結(jié)點(diǎn)的數(shù)據(jù)。
(3)通過XmlDocument類的CreateElement()和CreateAttribute()方法,創(chuàng)建新的元素結(jié)點(diǎn)和屬性結(jié)點(diǎn),并通過XmlNode的Name、Value、InnerText等屬性設(shè)置新結(jié)點(diǎn)的屬性。CreateElement()和CreateAttribute()的常用定義如下。
CreateElement(string name):創(chuàng)建具有指定限定名的元素結(jié)點(diǎn),其中name表示元素結(jié)點(diǎn)的限定名,返回XmlElement類型對(duì)象。
CreateAttribute(string name):創(chuàng)建具有指定限定名的屬性結(jié)點(diǎn),其中name表示屬性結(jié)點(diǎn)的限定名,返回XmlAttribute類型對(duì)象。
(4)通過XmlDocument類的CreateXmlDeclaration()方法創(chuàng)建一個(gè)XML文檔說明,并通過XmlDocument.AppendChild()方法添加到XML文檔中。CreateXmlDeclaration()的定義如下。
CreateXmlDeclaration(string version, string encoding, string standalone):創(chuàng)建一 個(gè)具有指定版本和編碼的XML文檔說明。其中,version表示版本,encoding表示XML文檔的編碼格式,默認(rèn)為utf-8,standalone表示是否在XML聲明上寫出獨(dú)立屬性,可選yes或no。
(5)通過XmlDocument類的CreateComment()方法創(chuàng)建一個(gè)具有指定文本的XML注釋,并通過XmlDocument.AppendChild()方法添加到XML文檔中。
CreateComment(string data):創(chuàng)建包含指定文本的XML注釋,其中data表示注釋的文本內(nèi)容。返回XmlComment類型對(duì)象。
(6)通過XmlDocument類的Save()方法保存一個(gè)XML文檔數(shù)據(jù)到文件或數(shù)據(jù)流,它包含以下重載版本:
Save(Stream sr):將內(nèi)存中的XML文檔數(shù)據(jù)保存到指定的數(shù)據(jù)流,其中,sr表示一個(gè)特定的可以寫入的數(shù)據(jù)流。
Save(string filename):將內(nèi)存中的XML文檔數(shù)據(jù)保存到指定的文件,其中,filename表示XML文件名。
Save(TextWriter tw):將內(nèi)存中的XML文檔數(shù)據(jù)保存到指定的文本數(shù)據(jù)寫入器,其中,tw表示一個(gè)文本寫入器對(duì)象。
Save(XmlWriter xw):將內(nèi)存中的XML文檔數(shù)據(jù)保存到指定的XML數(shù)據(jù)寫入器,其中,xw表示一個(gè)XML數(shù)據(jù)寫入器對(duì)象。
簡(jiǎn)單例子
寫入文檔:
static void Main(string[] args) { XmlDocument doc = new XmlDocument();//實(shí)例化文檔對(duì)象 if (File.Exists("student.xml"))//如果文件已存在,載入文檔 { doc.Load("student.xml"); } else//否則 { XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8","yes");//設(shè)置聲明 doc.AppendChild(dec); XmlElement root = doc.CreateElement("root");//加入根節(jié)點(diǎn) doc.AppendChild(root); } XmlElement student = doc.CreateElement("student");//插入一個(gè)student節(jié)點(diǎn) student.SetAttribute("id", "120");//設(shè)置id屬性 student.SetAttribute("age", "22");//設(shè)置age屬性 student.InnerText = "張三";//設(shè)置中間文本 doc.DocumentElement.AppendChild(student);//將student節(jié)點(diǎn)連接在根節(jié)點(diǎn)上 doc.Save("student.xml");//保存文檔 }
執(zhí)行3次后產(chǎn)生的xml文檔:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <root> <student id="120" age="22">張三</student> <student id="120" age="22">張三</student> <student id="120" age="22">張三</student> </root>
相關(guān)文章
c# 使用WebRequest實(shí)現(xiàn)多文件上傳
這篇文章主要介紹了c# 使用WebRequest實(shí)現(xiàn)多文件上傳的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03C# 大數(shù)據(jù)導(dǎo)出word的假死報(bào)錯(cuò)的處理方法
C# 大數(shù)據(jù)導(dǎo)出word的假死報(bào)錯(cuò)的處理方法,需要的朋友可以參考一下2013-03-03C#動(dòng)態(tài)創(chuàng)建button按鈕的方法實(shí)例詳解
這篇文章主要介紹了C#動(dòng)態(tài)創(chuàng)建button按鈕的方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器
這篇文章主要介紹了C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器的相關(guān)代碼和使用方法,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-07-07