C#使用XmlDocument操作XML進行查詢、增加、修改、刪除、儲存應用的執行個體

來源:互聯網
上載者:User

在.NET中使用DataSet來擷取XML資料與儲存XML資料很簡單,很好用,不過有一些複雜點的XML結構,使用DataSet來操作就沒有XmlDocument來操作這麼好用了,所以本文在C#使用XmlDocument來操作XML的查詢、增加、修改、刪除、儲存的基本操作。

下面看執行個體:

XML檔案:books.xml
 1<?xml version="1.0" encoding="UTF-8"?>
 2<books>
 3 <book>
 4  <name>哈裡傳輸速率</name>
 5  <price>10</price>
 6  <memo>這是一本很好看的書。</memo>
 7 </book>
 8 <book id="B02">
 9  <name>三國演義</name>
10  <price>10</price>
11  <memo>四大名著之一。</memo>
12 </book>
13 <book id="B03">
14  <name>水滸</name>
15  <price>6</price>
16  <memo>四大名著之一。</memo>
17 </book>
18 <book id="B04">
19  <name>紅樓</name>
20  <price>5</price>
21  <memo>四大名著之一。</memo>
22 </book>
23</books> 
下面是C#代碼:
1eusing System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Xml;
 5
 6namespace TestXml
 7   {
 8    class Program
 9       {
10        static void Main(string[] args)
11           {
12            XmlElement theBook = null, theElem = null, root = null;
13            XmlDocument xmldoc = new XmlDocument();
14            try
15               {
16                xmldoc.Load("Books.xml");
17                root = xmldoc.DocumentElement;
18
19                //---  建立一本書開始 ----
20                theBook = xmldoc.CreateElement("book");
21                theElem = xmldoc.CreateElement("name");
22                theElem.InnerText = "新書";
23                theBook.AppendChild(theElem);
24
25                theElem = xmldoc.CreateElement("price");
26                theElem.InnerText = "20";
27                theBook.AppendChild(theElem);
28
29                theElem = xmldoc.CreateElement("memo");
30                theElem.InnerText = "新書更好看。";
31                theBook.AppendChild(theElem);
32                root.AppendChild(theBook);
33                Console.Out.WriteLine("---  建立一本書開始 ----");
34                Console.Out.WriteLine(root.OuterXml);
35                //---  建立一本書完成 ----
36
37                //---  下面對《哈裡傳輸速率》做一些修改。 ----
38                //---  查詢找《哈裡傳輸速率》----
39                theBook = (XmlElement)root.SelectSingleNode("/books/book[name=''哈裡傳輸速率'']");
40                Console.Out.WriteLine("---  尋找《哈裡傳輸速率》 ----");
41                Console.Out.WriteLine(theBook.OuterXml);
42                //---  此時修改這本書的價格 -----
43                theBook.GetElementsByTagName("price").Item(0).InnerText = "15";//getElementsByTagName返回的是NodeList,所以要跟上item(0)
44                Console.Out.WriteLine("---  此時修改這本書的價格 ----");
45                Console.Out.WriteLine(theBook.OuterXml);
46                //---  另外還想加一個屬性id,值為B01 ----
47                theBook.SetAttribute("id", "B01");
48                Console.Out.WriteLine("---  另外還想加一個屬性id,值為B01 ----");
49                Console.Out.WriteLine(theBook.OuterXml);
50                //---  對《哈裡傳輸速率》修改完成。 ----
51
52                //---  再將所有價格低於10的書刪除  ----
53                theBook = (XmlElement)root.SelectSingleNode("/books/book[@id=''B02'']");
54                Console.Out.WriteLine("---  要用id屬性刪除《三國演義》這本書 ----");
55                Console.Out.WriteLine(theBook.OuterXml);
56                theBook.ParentNode.RemoveChild(theBook);
57                Console.Out.WriteLine("---  刪除後的XML ----");
58                Console.Out.WriteLine(xmldoc.OuterXml);
59
60                //---  再將所有價格低於10的書刪除  ----
61                XmlNodeList someBooks = root.SelectNodes("/books/book[price<10]");
62                Console.Out.WriteLine("---  再將所有價格低於10的書刪除  ---");
63                Console.Out.WriteLine("---  合格書有 "   someBooks.Count   "本。  ---");
64
65                for (int i = 0; i < someBooks.Count; i  )
66                {
67                    someBooks.Item(i).ParentNode.RemoveChild(someBooks.Item(i));
68                }
69                Console.Out.WriteLine("---  刪除後的XML ----");
70                Console.Out.WriteLine(xmldoc.OuterXml);
71
72                xmldoc.Save("books.xml");//儲存到books.xml
73
74                Console.In.Read();
75            }
76            catch (Exception e)
77            {
78                Console.Out.WriteLine(e.Message);
79            }
80        }
81    }
82}
上面C#代碼都有注釋,很簡單就看明白了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.