XML的基本用法
新建XML文檔
public static void CreateXML() {
/*
* XML:可擴展的標記語言
* 用於存儲數據,類似於小型數據庫
* 嚴格區分大小寫
* 標籤不固定
* 標籤成對出現
*/
//Test : 通過代碼創建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文檔節點中的屬性
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("刪除並保存成功");
}
我的另一篇關於學習XML後的實例Demo:
https://blog.csdn.net/shaotaiban1097/article/details/82723036