1. 程式人生 > >C#中的XML基本用法

C#中的XML基本用法

XML的基本用法

新建XML文件

public static void CreateXML() {
        /*
         * XML:可擴充套件的標記語言  
         *      用於儲存資料,類似於小型資料庫
         *      嚴格區分大小寫
         *      標籤不固定
         *      標籤成對出現
         */

        //Step1:引用名稱空間System.Xml;

        //Step2:建立XML文件物件
        XmlDocument doc = new XmlDocument();

        //Step3:建立第一行描述資訊,並新增到doc文件中  Element 與 Node的關係,文件中所有內容都屬於Element,其中標籤屬於Node,
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(dec);

        //Step4:XML必須包含且僅能包含一個根結點
        XmlElement order = doc.CreateElement("Order");
        doc.AppendChild(order);

        //Step5:給根結點建立子結點
        XmlElement customerName = doc.CreateElement("customerName");
        customerName.InnerText = "張三";
        order.AppendChild(customerName);

        //Step6:給customerName新增子結點
        XmlElement customerNumber = doc.CreateElement("customerNumber");
        customerNumber.InnerText = "100000001";
        order.AppendChild(customerNumber);

        XmlElement items = doc.CreateElement("items");
        order.AppendChild(items);

        //Step6:給結點新增屬性
        XmlElement orderItem1 = doc.CreateElement("OrderItems");
        orderItem1.SetAttribute("Name", "手機");
        orderItem1.SetAttribute("Count", "1");
        items.AppendChild(orderItem1);

        XmlElement orderItem2 = doc.CreateElement("OrderItems");
        orderItem2.SetAttribute("Name", "手錶");
        orderItem2.SetAttribute("Count", "2");
        items.AppendChild(orderItem2);

        XmlElement orderItem3 = doc.CreateElement("OrderItems");
        orderItem3.SetAttribute("Name", "手環");
        orderItem3.SetAttribute("Count", "3");
        items.AppendChild(orderItem3);

        doc.Save("books.xml");
        Console.WriteLine("儲存成功");
        //如果沒有顯示。儲存路徑 -- 解決方案- 顯示所有檔案(該按鈕有可能需要點"更多"箭頭) - bin - debug
    }

追加XML文件

    public static void AppendXML() {

        //追加XML文件
        XmlDocument doc = new XmlDocument();
        //如果檔案存在 -- 追加
        //如果檔案不存在 -- 建立
        if (File.Exists("books.xml")) {
            //載入XML
            doc.Load("books.xml");//bin目錄下
            //獲取檔案根結點
            XmlElement books = doc.DocumentElement;
            //然後追加子結點
            XmlElement other = doc.CreateElement("Other");
            books.AppendChild(other);
        } else {
            //建立XML文件第一行
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            //建立根結點
            XmlElement books = doc.CreateElement("Books");
            doc.AppendChild(books);
        }
        doc.Save("books.xml");
        Console.WriteLine("儲存成功");
    }

載入XML文件的節點

    public static void LoadXML() {

        XmlDocument doc = new XmlDocument();

        //載入要讀取的XML文件
        doc.Load("books.xml");
        //獲取根結點
        XmlElement books = doc.DocumentElement;

        //獲得子結點,返回結點的集合
        XmlNodeList xnl = books.ChildNodes;

        foreach (XmlNode item in xnl) {
            Console.WriteLine(item.InnerText);
        }
    }

讀取XML文件節點中的屬性

    //讀取XML文件中的屬性
    public static void LoadAttribute() {

        XmlDocument doc = new XmlDocument();
        doc.Load("books.xml");

        //獲取結點集合
        XmlNodeList xmlNodeList = doc.SelectNodes("/Order/Items/OrderItem");
        foreach (XmlNode node in xmlNodeList) {
            //獲取屬性
            Console.WriteLine(node.Attributes["name"].Value);

            Console.WriteLine(node.Attributes["Count"].Value);
        }
    }

刪除XML文件的節點

    //刪除結點
    public static void DeleteNode() {

        XmlDocument doc = new XmlDocument();
        doc.Load("books.xml");

        //獲取單個結點
        XmlNode xn = doc.SelectSingleNode("/Order/Items");

        //刪除結點
        xn.RemoveAll();

        //儲存
        doc.Save("books.xml");
        Console.WriteLine("刪除並儲存成功");
    }
}