本文以 .NET 的 XmlDocument 為例,示範如何對同一份 XML 檔案完成增、刪、改、查四種基本操作。範例使用的資料檔 bookstore.XML 內容如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>
整體流程是「載入 → 定位父節點 → 建立新節點與子節點 → 掛上父節點 → 存檔」。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("bookstore.XML");
XmlNode root = xmlDoc.SelectSingleNode("bookstore"); // 找到 <bookstore>
XmlElement xe1 = xmlDoc.CreateElement("book"); // 建立 <book> 節點
xe1.SetAttribute("genre", "李贊紅"); // 設定 genre 屬性
xe1.SetAttribute("ISBN", "2-3631-4"); // 設定 ISBN 屬性
XmlElement xesub1 = xmlDoc.CreateElement("title");
xesub1.InnerText = "CS從入門到精通"; // 文字節點
xe1.AppendChild(xesub1); // 掛到 <book> 底下
XmlElement xesub2 = xmlDoc.CreateElement("author");
xesub2.InnerText = "候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("price");
xesub3.InnerText = "58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1); // 掛到 <bookstore> 底下
xmlDoc.Save("bookstore.XML");
執行後的結果:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="李贊紅" ISBN="2-3631-4">
<title>CS從入門到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>
需求是把 genre 屬性值為「李贊紅」的節點改成「update李贊紅」,並把該節點下 <author> 的文字改成「亞勝」。做法是先取得所有子節點,逐一比對屬性,命中後再往下一層找。
XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach (XmlNode xn in nodeList) // 走訪所有子節點
{
XmlElement xe = (XmlElement)xn; // 轉型為 XmlElement
if (xe.GetAttribute("genre") == "李贊紅")
{
xe.SetAttribute("genre", "update李贊紅");
XmlNodeList nls = xe.ChildNodes; // 再取得這一層的子節點
foreach (XmlNode xn1 in nls)
{
XmlElement xe2 = (XmlElement)xn1;
if (xe2.Name == "author")
{
xe2.InnerText = "亞勝";
break; // 找到就跳出
}
}
break; // 已處理完畢,不再往下找
}
}
xmlDoc.Save("bookstore.XML");
最後的檔案內容:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="update李贊紅" ISBN="2-3631-4">
<title>CS從入門到精通</title>
<author>亞勝</author>
<price>58.3</price>
</book>
</bookstore>
這裡示範兩種不同層級的刪除:把 genre="fantasy" 節點的 genre 屬性拿掉,再把 genre="update李贊紅" 的整個節點清空。
XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == "fantasy")
{
xe.RemoveAttribute("genre"); // 只刪屬性
}
else if (xe.GetAttribute("genre") == "update李贊紅")
{
xe.RemoveAll(); // 刪掉節點全部內容
}
}
xmlDoc.Save("bookstore.XML");
結果如下(第二個 book 只剩一對空標籤):
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>
實務上要整筆移除節點,除了 RemoveAll() 清內容,更乾淨的做法是取得父節點後呼叫 ParentNode.RemoveChild(xe),讓空標籤也一併消失。
XmlNode xn = xmlDoc.SelectSingleNode("bookstore");
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre")); // 印出屬性值
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeList xnf1 = xe.ChildNodes;
foreach (XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText); // 印出子節點文字
}
}
four 個操作共用同一套骨架:SelectSingleNode 定位、CreateElement 產生節點、AppendChild 建立階層、Save 落盤。把這幾個方法記熟,XML 的日常讀寫就足夠應付了。