C#代碼讀寫XML

來源:互聯網
上載者:User

標籤:

<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文檔            //建立XML文檔描述。即:建立<?xml version="1.0" encoding="utf-8" ?>這段            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);            //將XML文檔描述添加到XML文檔中            doc.AppendChild(dec);            //建立XML文檔的根節點 ------我這裡將XML文檔的根節點取名為<Books>            XmlElement books = doc.CreateElement("Books");            //將剛剛建立的根節點添加到這個XML文檔中            doc.AppendChild(books);            //--------------------------------給Books這個根節點添加一個Book子節點,取名boo1            //給XML文檔的根節點Books建立子節點            XmlElement book1 = doc.CreateElement("Book");            //將跟book1添加到根節點中            books.AppendChild(book1);            //給book1添加一個Name的子節點            XmlElement name1 = doc.CreateElement("Name");            //給name1這個子節點賦值            name1.InnerText = "西遊記";            //將name1添加到Boo1中            book1.AppendChild(name1);            //給book1添加一個Price的子節點            XmlElement price1 = doc.CreateElement("Price");            //給price1這個子節點賦值            price1.InnerText = "50";            //給price1這個子節點添加到book1中            book1.AppendChild(price1);            //給book1添加一個Des的子節點            XmlElement des1 = doc.CreateElement("Des");            //給des1這個子節點賦值            des1.InnerText = "神話故事";            //給des1這個子節點添加到book1中            book1.AppendChild(des1);            //--------------------------------給Books這個根節點添加另外一個Book子節點,取名boo2            //給XML文檔的根節點Books建立子節點            XmlElement book2 = doc.CreateElement("Book");            //將跟book1添加到根節點中            books.AppendChild(book2);            //給book1添加一個Name的子節點            XmlElement name2 = doc.CreateElement("Name");            //給name1這個子節點賦值            name2.InnerText = "紅樓夢";            //將name1添加到Boo1中            book2.AppendChild(name2);            //給book1添加一個Price的子節點            XmlElement price2 = doc.CreateElement("Price");            //給price1這個子節點賦值            price2.InnerText = "60";            //給price1這個子節點添加到book1中            book2.AppendChild(price2);            //給book1添加一個Des的子節點            XmlElement des2 = doc.CreateElement("Des");            //給des1這個子節點賦值            des2.InnerText = "曆史題材";            //給des1這個子節點添加到book1中            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子節點,取名customerName            //給XML文檔的根節點Order建立子節點customerName            XmlElement customerName = doc.CreateElement("CustomerName");            //給customerName這個子節點賦值            customerName.InnerText = "張學友";            //將customerName這個子節點添加到根節點下            order.AppendChild(customerName);            //給XML文檔的根節點Order建立子節點orderNumber            XmlElement orderNumber = doc.CreateElement("OrderNumber");            //給orderNumber這個子節點賦值            orderNumber.InnerText = "dd00001";            //將orderNumber這個子節點添加到根節點下            order.AppendChild(orderNumber);            //給XML文檔的根節點Order建立子節點items            XmlElement items = doc.CreateElement("Items");            //將items這個子節點添加到根節點下            order.AppendChild(items);            //建立一個orderItem1的節點            XmlElement orderItem1 = doc.CreateElement("OrderItem");            //給orderItem1這個節點添加屬性            orderItem1.SetAttribute("Name", "手機");            orderItem1.SetAttribute("Count", "1");            //將orderItem1這個節點添加到items這個節點下            items.AppendChild(orderItem1);            //再建立一個orderItem2的節點            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代碼的具體內容



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

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.