C# 操作XML

使用xml時,一定要引用命名空間:using System.Xml;

  1. 創建不帶屬性的xml文檔
             // (1)創建xml文檔對象
            XmlDocument doc = new XmlDocument();

            // (2)創建描述信息(第1行)
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);

            // (3)創建根節點(Books)
            XmlElement books = doc.CreateElement("Books");
            doc.AppendChild(books);

            // (4)給根節點Books創建第1個子節點
            XmlElement book1 = doc.CreateElement("Book");
            books.AppendChild(book1);

            XmlElement name1 = doc.CreateElement("Name");
            name1.InnerText = "紅樓夢";
            book1.AppendChild(name1);

            XmlElement writer1 = doc.CreateElement("Writer");
            writer1.InnerXml = "曹雪芹";
            book1.AppendChild(writer1);

            XmlElement price1 = doc.CreateElement("Price");
            price1.InnerText = "25";
            book1.AppendChild(price1);


            // (5)給根節點Books創建第2個子節點
            XmlElement book2 = doc.CreateElement("Book");
            books.AppendChild(book2);

            XmlElement name2 = doc.CreateElement("Name");
            name2.InnerText = "西遊記";
            book2.AppendChild(name2);

            XmlElement writer2 = doc.CreateElement("Writer");
            writer2.InnerXml = "吳承恩";
            book2.AppendChild(writer2);

            XmlElement price2 = doc.CreateElement("Price");
            price2.InnerText = "20";
            book2.AppendChild(price2);


            // (5)保存到doc文檔中
            doc.Save("Books.xml");
            MessageBox.Show("保存成功");


效果如下:

<?xml version="1.0" encoding="utf-8"?>
<Books>
  <Book>
    <Name>紅樓夢</Name>
    <Writer>曹雪芹</Writer>
    <Price>25</Price>
  </Book>
  <Book>
    <Name>西遊記</Name>
    <Writer>吳承恩</Writer>
    <Price>20</Price>
  </Book>
</Books>
  1. 創建帶屬性的xml文檔
            // (1)創建xml文檔對象
            XmlDocument doc = new XmlDocument();

            // (2)創建描述信息(第1行)
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);

            // (3)創建根節點(Order),且保存
            XmlElement order = doc.CreateElement("Order");
            doc.AppendChild(order);

            // (4)給根節點Books創建第1個子節點
            XmlElement customrName = doc.CreateElement("CustomrName");
            customrName.InnerText = "Jom";
            order.AppendChild(customrName);

            XmlElement carName = doc.CreateElement("CarName");
            carName.InnerText = "奧迪";
            order.AppendChild(carName);

            XmlElement price = doc.CreateElement("Price");
            price.InnerText = "50萬";
            order.AppendChild(price);



            // (5)給根節點Books創建第2個子節點
            XmlElement items = doc.CreateElement("Items");
            order.AppendChild(items);
            XmlElement orderItem2 = doc.CreateElement("OrderItem");

            orderItem2.SetAttribute("Name", "手機");   //給節點添加屬性
            orderItem2.SetAttribute("Count", "10");
            items.AppendChild(orderItem2);


            XmlElement orderItem3 = doc.CreateElement("OrderItem");
            orderItem3.SetAttribute("Name", "平板");   //給節點添加屬性
            orderItem3.SetAttribute("Count", "15");
            items.AppendChild(orderItem3);


            // (5)保存到doc文檔中
            doc.Save("Order.xml");
            MessageBox.Show("保存成功");

效果如下:

<?xml version="1.0" encoding="utf-8"?>
<Order>
  <CustomrName>Jom</CustomrName>
  <CarName>奧迪</CarName>
  <Price>50萬</Price>
  <Items>
    <OrderItem Name="手機" Count="10" />
    <OrderItem Name="平板" Count="15" />
  </Items>
</Order>
  1. 刪除Items節點下的所有元素
            XmlDocument doc = new XmlDocument();
            doc.Load("Order.xml");

            XmlNode xn = doc.SelectSingleNode("/Order/Items");
            xn.RemoveAll();
            doc.Save("Order.xml");  //刪除後重新保存一下

刪除前:

<?xml version="1.0" encoding="utf-8"?>
<Order>
  <CustomrName>Jom</CustomrName>
  <CarName>奧迪</CarName>
  <Price>50萬</Price>
  <Items>
    <OrderItem Name="手機" Count="10" />
    <OrderItem Name="平板" Count="15" />
  </Items>
</Order>

刪除後:

<?xml version="1.0" encoding="utf-8"?>
<Order>
  <CustomrName>Jom</CustomrName>
  <CarName>奧迪</CarName>
  <Price>50萬</Price>
  <Items>
  </Items>
</Order>
  1. 讀取不帶屬性的xml文檔
            XmlDocument doc = new XmlDocument();
            //加載要讀取的xml
            doc.Load("Books.xml");

            //獲取根節點
            XmlElement books = doc.DocumentElement;

            //獲取子節點 返回節點集合
            XmlNodeList xnl = books.ChildNodes;

            foreach (XmlNode item in xnl)
            {
                textBox1.AppendText(item.InnerText + "\r\n");
            }

xml文檔

<?xml version="1.0" encoding="utf-8"?>
<Books>
  <Book>
    <Name>紅樓夢</Name>
    <Writer>曹雪芹</Writer>
    <Price>25</Price>
  </Book>
  <Book>
    <Name>西遊記</Name>
    <Writer>吳承恩</Writer>
    <Price>20</Price>
  </Book>
</Books>

效果如下:
紅樓夢曹雪芹25
西遊記吳承恩20

  1. 讀取帶屬性的xml文檔
            XmlDocument doc = new XmlDocument();
            doc.Load("Order.xml");
            XmlNodeList xnl = doc.SelectNodes("/Order/Items/OrderItem");

            foreach (XmlNode node in xnl)
            {
                textBox2.AppendText(node.Attributes["Name"].Value + "\r\n");
                textBox2.AppendText(node.Attributes["Count"].Value + "\r\n");

            }

xml文檔

<?xml version="1.0" encoding="utf-8"?>
<Order>
  <CustomrName>Jom</CustomrName>
  <CarName>奧迪</CarName>
  <Price>50萬</Price>
  <Items>
    <OrderItem Name="手機" Count="10" />
    <OrderItem Name="平板" Count="15" />
  </Items>
</Order>

效果如下:
手機
10
平板
15

最後再介紹一種常用的方法:

讀取name節點的元素:

<?xml version="1.0" encoding="utf-8"?>
<school>
  <name>崑山一中</name>
  <addr>蘇州市崑山市馬鞍山中路619號</addr>
  <tel>0512-123456789</tel>
</school>

程序如下:

            /* 步驟:
             * 1.實例化一個xml對象
             * 2.加載一個xml文檔到這個對象
             * 3.獲取根節點
             * 4.在此根節點上根據參數查找子節點
             * 5.獲取子節點的元素
             */


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

            XmlNode root = doc.DocumentElement;
            XmlNode name = root.SelectSingleNode("//name");
            if (name != null)
            {
                string schoolName = name.InnerText;  //結果:崑山一中

            }

源文件下載地址:
鏈接:https://pan.baidu.com/s/1djYSwlLCYw7s1jF-PRFSTw
提取碼:h3au

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章