C#如何操作XML文件


⒈XML?

  XML是一種可擴展的標記語言

  具有以下特點

    1.嚴格區分大小寫

    2.標簽成對出現

    3.有且只有一個根節點

⒉XML的創建

1 <?xml version="1.0" encoding="utf-8" ?>
2 <Order>
3   <CustomerName>fanqi</CustomerName>
4   <Items>
5     <OrderItem Name="Money" count="100000000000000"/>
6   </Items>
7 </Order> 
 1         public static void create()
 2         {
 3             XmlDocument doc = new XmlDocument();
 4             XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
 5             doc.AppendChild(dec);   //將版本和編碼信息添加到文檔的第一行
 6 
 7             XmlElement order = doc.CreateElement("Order");
 8             doc.AppendChild(order); //添加一個根級目錄
 9 
10             XmlElement customerName = doc.CreateElement("CustomerName");
11             customerName.InnerText = "fanqi";   //InnerText放文本時使用,特殊字符會轉義
12             //customerName.InnerXml = "<h1> haha </h1>";    //InnerXml放標簽時使用
13             order.AppendChild(customerName);
14 
15             XmlElement items = doc.CreateElement("Items");
16             order.AppendChild(items);
17 
18             XmlElement orderitem = doc.CreateElement("OrderItem");
19             orderitem.SetAttribute("Name", "Money");
20             orderitem.SetAttribute("count", "100000000000000");
21             items.AppendChild(orderitem);
22 
23             doc.Save("fanqi.xml");
24         }

⒊XML追加

 1         public static void add()
 2         {
 3             XmlDocument doc = new XmlDocument();
 4             if (File.Exists("fanqi.xml"))
 5             {
 6                 doc.Load("fanqi.xml");
 7                 XmlElement books = doc.DocumentElement; //獲得文件的根節點
 8             }
 9             else
10             {
11                 //不存在就創建唄
12             }
13             doc.Save("fanqi.xml");
14         }

⒋讀取不帶屬性的XML

 1         public static void loadNoAttribute()
 2         {
 3             XmlDocument doc = new XmlDocument();
 4             if (File.Exists("fanqi.xml"))
 5             {
 6                 doc.Load("fanqi.xml");
 7                 XmlElement order = doc.DocumentElement; //獲得文件的根節點
 8                 XmlNodeList nodeList = order.ChildNodes;
 9                 foreach(XmlNode item in nodeList)   //遍歷輸出子節點內容
10                 {
11                     Console.WriteLine(item.InnerText);
12                 }
13             }
14         }

⒌讀取帶有屬性的XML

 1         public static void loadAttribute()
 2         {
 3             XmlDocument doc = new XmlDocument();
 4             if (File.Exists("fanqi.xml"))
 5             {
 6                 doc.Load("fanqi.xml");
 7                 XmlNodeList nodeList = doc.SelectNodes("/Order/Items/OrderItem");
 8                 foreach (XmlNode item in nodeList)
 9                 {
10                     Console.WriteLine(item.Attributes["Name"].Value);
11                     Console.WriteLine(item.Attributes["count"].Value);
12                 }
13             }
14         }

⒍更改XML屬性的值

 1         public static void modifyAttribute()
 2         {
 3             XmlDocument doc = new XmlDocument();
 4             if (File.Exists("fanqi.xml"))
 5             {
 6                 doc.Load("fanqi.xml");
 7                 XmlNode xn = doc.SelectSingleNode("/Order/Items/OrderItem[@Name='fanqi']");
 8                 xn.Attributes["count"].Value = "999999999999";
 9                 doc.Save("fanqi.xml");
10             }
11         }

⒎刪除節點

 1         public static void removeNode()
 2         {
 3             XmlDocument doc = new XmlDocument();
 4             if (File.Exists("fanqi.xml"))
 5             {
 6                 doc.Load("fanqi.xml");
 7                 XmlNode xn = doc.SelectSingleNode("/Order/Items");
 8                 xn.RemoveAll();
 9                 doc.Save("fanqi.xml");
10             }
11         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM