C#代碼操作XML進(jìn)行增、刪、改操作
作為一個(gè)小型的數(shù)據(jù)存儲傳遞的工具——XML,大家肯定也不陌生,今天就關(guān)于XML的一些簡單操作做些總結(jié)。
這些都是在控制臺進(jìn)行操作的·····
1.創(chuàng)建XML
1)創(chuàng)建普通XML
static void Main(string[] args) { //通過代碼來創(chuàng)建XML文檔 //1、引用命名空間 //2、創(chuàng)建XML文檔對象 XmlDocument doc = new XmlDocument(); //3、創(chuàng)建第一個(gè)行描述信息,并且添加到doc文檔中 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); //4、創(chuàng)建根節(jié)點(diǎn) XmlElement books = doc.CreateElement("Books"); //將根節(jié)點(diǎn)添加到文檔中 doc.AppendChild(books); //5、給根節(jié)點(diǎn)Books創(chuàng)建子節(jié)點(diǎn) XmlElement book1 = doc.CreateElement("Book"); //將book添加到根節(jié)點(diǎn) books.AppendChild(book1); //6、給Book1添加子節(jié)點(diǎn) XmlElement name1 = doc.CreateElement("Name"); name1.InnerText = "三國演義"; book1.AppendChild(name1); XmlElement price1 = doc.CreateElement("Price"); price1.InnerText = "70"; book1.AppendChild(price1); XmlElement des1 = doc.CreateElement("Des"); des1.InnerText = "好看"; book1.AppendChild(des1); XmlElement book2 = doc.CreateElement("Book"); books.AppendChild(book2); XmlElement name2 = doc.CreateElement("Name"); name2.InnerText = "西游記"; book2.AppendChild(name2); XmlElement price2= doc.CreateElement("Price"); price2.InnerText = "80"; book2.AppendChild(price2); XmlElement des2 = doc.CreateElement("Des"); des2.InnerText = "還不錯(cuò)"; book2.AppendChild(des2); doc.Save("Books.xml"); Console.WriteLine("保存成功"); Console.ReadKey(); }
根據(jù)代碼寫的,然后運(yùn)行,就會得到我們想要的XML文檔:
2)創(chuàng)建帶屬性的XML
static void Main(string[] args) { XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8","yes"); doc.AppendChild(dec); XmlElement order = doc.CreateElement("Order"); doc.AppendChild(order); XmlElement customerName = doc.CreateElement("CustomerName"); customerName.InnerText = "張三"; order.AppendChild(customerName); XmlElement customerNumber = doc.CreateElement("CustomerNumber"); customerNumber.InnerText = "1010101"; order.AppendChild(customerNumber); XmlElement items = doc.CreateElement("Items"); order.AppendChild(items); XmlElement orderItem1 = doc.CreateElement("OrderItem"); //給節(jié)點(diǎn)添加屬性 orderItem1.SetAttribute("Name", "單反"); orderItem1.SetAttribute("Count", "1120"); items.AppendChild(orderItem1); XmlElement orderItem2 = doc.CreateElement("OrderItem"); //給節(jié)點(diǎn)添加屬性 orderItem2.SetAttribute("Name", "書"); orderItem2.SetAttribute("Count", "30"); items.AppendChild(orderItem2); XmlElement orderItem3 = doc.CreateElement("OrderItem"); //給節(jié)點(diǎn)添加屬性 orderItem3.SetAttribute("Name", "手機(jī)"); orderItem3.SetAttribute("Count", "2000"); items.AppendChild(orderItem3); doc.Save("Order.xml"); Console.WriteLine("保存成功"); Console.ReadKey(); }
根據(jù)代碼寫的,然后運(yùn)行,就會得到我們想要的XML文檔:
2.追加XML
static void Main(string[] args) { //追加XML文檔 XmlDocument doc = new XmlDocument(); XmlElement books; if (File.Exists("Books.xml")) { //如果文件存在 加載XML doc.Load("Books.xml"); //獲得文件的根節(jié)點(diǎn) books = doc.DocumentElement; } else { //如果文件不存在 //創(chuàng)建第一行 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); //創(chuàng)建跟節(jié)點(diǎn) books = doc.CreateElement("Books"); doc.AppendChild(books); } //5、給根節(jié)點(diǎn)Books創(chuàng)建子節(jié)點(diǎn) XmlElement book1 = doc.CreateElement("Book"); //將book添加到根節(jié)點(diǎn) books.AppendChild(book1); //6、給Book1添加子節(jié)點(diǎn) XmlElement name1 = doc.CreateElement("Name"); name1.InnerText = "c#開發(fā)大全"; book1.AppendChild(name1); XmlElement price1 = doc.CreateElement("Price"); price1.InnerText = "110"; book1.AppendChild(price1); XmlElement des1 = doc.CreateElement("Des"); des1.InnerText = "看不懂"; book1.AppendChild(des1); doc.Save("Books.xml"); Console.WriteLine("保存成功"); Console.ReadKey(); }
根據(jù)代碼寫的,然后運(yùn)行,就會得到我們想要的XML文檔:
3.讀取XML
1)讀取普通XML
static void Main(string[] args) { XmlDocument doc = new XmlDocument(); //加載要讀取的XML doc.Load("Books.xml"); //獲得根節(jié)點(diǎn) XmlElement books = doc.DocumentElement; //獲得子節(jié)點(diǎn) 返回節(jié)點(diǎn)的集合 XmlNodeList xnl = books.ChildNodes; foreach (XmlNode item in xnl) { Console.WriteLine(item.InnerText); } Console.ReadKey(); }
根據(jù)代碼寫的,然后運(yùn)行,就會得到讀取的XML結(jié)果:
2)讀取帶屬性的XML
static void Main(string[] args) { //讀取帶屬性的XML文檔 XmlDocument doc = new XmlDocument(); doc.Load("Order.xml"); XmlNodeList xnl = doc.SelectNodes("/Order/Items/OrderItem"); foreach (XmlNode node in xnl) { Console.WriteLine(node.Attributes["Name"].Value); Console.WriteLine(node.Attributes["Count"].Value); } Console.ReadKey(); }
根據(jù)代碼寫的,然后運(yùn)行,就會得到讀取的XML結(jié)果:
4.修改屬性的值
static void Main(string[] args) { //改變屬性的值 XmlDocument doc = new XmlDocument(); doc.Load("Order.xml"); XmlNode xn = doc.SelectSingleNode("/Order/Items/OrderItem[@Name='單反']"); xn.Attributes["Count"].Value = "2000"; xn.Attributes["Name"].Value = "電腦"; doc.Save("Order.xml"); Console.WriteLine("保存成功"); Console.ReadKey(); }
根據(jù)代碼寫的,然后運(yùn)行,就會得到修改后的XML結(jié)果:
5.刪除XML節(jié)點(diǎn)
static void Main(string[] args) { XmlDocument doc = new XmlDocument(); doc.Load("Order.xml"); XmlNode xn = doc.SelectSingleNode("/Order/Items"); xn.RemoveAll(); doc.Save("Order.xml"); Console.WriteLine("刪除成功"); Console.ReadKey(); }
根據(jù)代碼寫的,然后運(yùn)行,就會得到修刪除后的XML結(jié)果:
至此:XML簡單的增刪改操作就結(jié)束了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件
這篇文章主要為大家詳細(xì)介紹了C#如何在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以學(xué)習(xí)一下2023-10-10C#使用Shader實(shí)現(xiàn)夜幕降臨倒計(jì)時(shí)的效果
這篇文章主要介紹了C#使用Shader實(shí)現(xiàn)夜幕降臨倒計(jì)時(shí)的效果,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10C#使用FluentHttpClient實(shí)現(xiàn)請求WebApi
FluentHttpClient 是一個(gè)REST API 異步調(diào)用 HTTP 客戶端,調(diào)用過程非常便捷,下面我們就來學(xué)習(xí)一下C#如何使用FluentHttpClient實(shí)現(xiàn)請求WebApi吧2023-12-12