C#代碼讀寫XML

來源:互聯網
上載者:User

標籤:space   ring   att   ldo   key   task   abc   pop   節點   

<1>

建立XML文檔

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;namespace 建立XML{    class Program    {        static void Main(string[] args)        {            XmlDocument doc = new XmlDocument(); //建立XML文檔            //--------------------------------給doc這個XML文檔加入文檔描寫敘述            //建立XML文檔描寫敘述。

即:建立<?

xml version="1.0" encoding="utf-8" ?>這段 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); //將XML文檔描寫敘述加入到XML文檔中 doc.AppendChild(dec); //--------------------------------給doc這個XML文檔加入一個根節點 //建立XML文檔的根節點 ------我這裡將XML文檔的根節點設定為<Books> XmlElement books = doc.CreateElement("Books"); //將剛剛建立的根節點加入到這個XML文檔中 doc.AppendChild(books); //--------------------------------給Books這個根節點加入一個Book子節點 //給XML文檔的根節點Books建立子節點 XmlElement book1 = doc.CreateElement("Book"); //將跟book1加入到根節點中 books.AppendChild(book1); //---------------給Book加入一個Name的子節點 XmlElement name1 = doc.CreateElement("Name"); //給name1這個子節點賦值 name1.InnerText = "西遊記"; //將name1加入到Boo1中 book1.AppendChild(name1); //---------------給Book加入一個Price的子節點 XmlElement price1 = doc.CreateElement("Price"); //給price1這個子節點賦值 price1.InnerText = "50"; //給price1這個子節點加入到book1中 book1.AppendChild(price1); //---------------給Book加入一個Des的子節點 XmlElement des1 = doc.CreateElement("Des"); //給des1這個子節點賦值 des1.InnerText = "神話故事"; //給des1這個子節點加入到book1中 book1.AppendChild(des1); //--------------------------------再給Books這個根節點加入一個Book子節點 XmlElement book2 = doc.CreateElement("Book"); //將跟book2加入到根節點中 books.AppendChild(book2); //---------------給Book加入一個Name的子節點 XmlElement name2 = doc.CreateElement("Name"); //給name2這個子節點賦值 name2.InnerText = "紅樓夢"; //將name2加入到book2中 book2.AppendChild(name2); //---------------給Book加入一個Price的子節點 XmlElement price2 = doc.CreateElement("Price"); //給price2這個子節點賦值 price2.InnerText = "60"; //給price2這個子節點加入到book2中 book2.AppendChild(price2); //---------------給Book加入一個Des的子節點 XmlElement des2 = doc.CreateElement("Des"); //給des2這個子節點賦值 des2.InnerText = "曆史題材"; //給des2這個子節點加入到book2中 book2.AppendChild(des2); doc.Save("Books.xml"); Console.WriteLine("儲存成功"); Console.ReadKey(); } }}


上面的代碼建立的XML文檔的詳細內容


 建立帶有屬性的XML文檔

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;namespace 建立帶屬性的XML{    class Program    {        static void Main(string[] args)        {            XmlDocument doc = new XmlDocument();//建立XML文檔            //建立XML文檔描寫敘述。即:建立<?

xml version="1.0" encoding="utf-8" ?

>這段 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); //將XML文檔描寫敘述加入到XML文檔中 doc.AppendChild(dec); //建立XML文檔的根節點 ------我這裡將XML文檔的根節點設定為<Order> XmlElement order = doc.CreateElement("Order"); //將剛剛建立的根節點加入到這個XML文檔中 doc.AppendChild(order); //--------------------------------給Order這個根節點加入一個CustomerName子節點 //給XML文檔的根節點Order建立子節點customerName XmlElement customerName = doc.CreateElement("CustomerName"); //給customerName這個子節點賦值 customerName.InnerText = "張學友"; //將customerName這個子節點加入到根節點下 order.AppendChild(customerName); //--------------------------------給Order這個根節點加入一個OrderNumber子節點 //給XML文檔的根節點Order建立子節點orderNumber XmlElement orderNumber = doc.CreateElement("OrderNumber"); //給orderNumber這個子節點賦值 orderNumber.InnerText = "dd00001"; //將orderNumber這個子節點加入到根節點下 order.AppendChild(orderNumber); //--------------------------------給Order這個根節點加入一個Items子節點 //給XML文檔的根節點Order建立子節點items XmlElement items = doc.CreateElement("Items"); //將items這個子節點加入到根節點下 order.AppendChild(items); //-------給Items加入一個子節點OrderItem XmlElement orderItem1 = doc.CreateElement("OrderItem"); //給orderItem1這個節點加入屬性 orderItem1.SetAttribute("Name", "手機"); orderItem1.SetAttribute("Count", "1"); //將orderItem1這個節點加入到items這個節點下 items.AppendChild(orderItem1); //-------再給Items加入一個子節點OrderItem XmlElement orderItem2 = doc.CreateElement("OrderItem"); //給orderItem2這個節點加入屬性 orderItem2.SetAttribute("Name", "電腦"); orderItem2.SetAttribute("Count", "2"); //將orderItem2這個節點加入到items這個節點下 items.AppendChild(orderItem2); doc.Save("Order.xml"); Console.WriteLine("儲存成功"); Console.ReadKey(); } }}

上面代碼建立帶有屬性的XML代碼的詳細內容


給標籤賦值: InnerText與InnerXml的差別

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;namespace 給節點加入內容_InnerText與InnerXml的差別{    class Program    {        static void Main(string[] args)        {            XmlDocument doc = new XmlDocument();            XmlDeclaration dec =doc.CreateXmlDeclaration("1.0", "utf-8", null);            doc.AppendChild(dec);            XmlElement books = doc.CreateElement("Books");            doc.AppendChild(books);            XmlElement book = doc.CreateElement("Book");            books.AppendChild(book);            //--------------------以下示範一下InnerText與 InnerXml的差別            XmlElement name1 = doc.CreateElement("Name1");            name1.InnerText = "<ABC>西遊記</ABC>";            book.AppendChild(name1);            XmlElement name2 = doc.CreateElement("Name2");            name2.InnerXml = "<ABC>紅樓夢</ABC>";            book.AppendChild(name2);            doc.Save("Books.xml");            Console.WriteLine("儲存成功");            Console.ReadKey();                               }    }}



C#代碼讀寫XML

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.