XML简单的增改删操作
XML文件的簡單增改刪,每一個都可以單獨拿出來使用。
新創建XML文件,<?xmlversion="1.0"encoding="utf-8"?>
<bookstore>
? <bookgenre="fantasy"ISBN="2-3631-4">
??? <title>Oberon's Legacy</title>
??? <author>Corets, Eva</author>
??? <price>5.95</price>
? </book>
</bookstore>
?
實現如下:
//插入節點
??????? protected void btn_Add_Click(object sender, EventArgs e)
??????? {
??????????? XmlDocument doc = new XmlDocument();
??????????? doc.Load(@"E:\ruxiaomeng\Simple\C++\CLR\stlclr\StlClr Sample\XMLTest\bookstore.xml");
?
??????????? XmlNode root = doc.SelectSingleNode("bookstore");//找到根節點
??????????? XmlElement new_ele = doc.CreateElement("book");//文檔創建節點<book>
??????????? new_ele.SetAttribute("genre", "歷史");
??????????? new_ele.SetAttribute("ISBN", "100-001-*6963");//設置屬性
?
??????????? XmlElement new_ele_childone = doc.CreateElement("title");
??????????? new_ele_childone.InnerText = "史記"; //填充新節點內的文本。
??????????? new_ele.AppendChild(new_ele_childone);//給父節點添加子節點。
?
??????????? XmlElement new_ele_childtwo = doc.CreateElement("author");
??????????? new_ele_childtwo.InnerText = "司馬遷";
??????????? new_ele.AppendChild(new_ele_childtwo);
?
??????????? XmlElement new_ele_childthree = doc.CreateElement("price");
??????????? new_ele_childthree.InnerText = "90.36";
??????????? new_ele.AppendChild(new_ele_childthree);
?
??????????? root.AppendChild(new_ele);//根節點添加新創建的子節點!
??????????? doc.Save(@"E:\ruxiaomeng\Simple\C++\CLR\stlclr\StlClr Sample\XMLTest\bookstore.xml");//記得一定要保存!
?
??????? }
?
??????? //更新屬性和節點值
??????? protected void btn_Edit_Click(object sender, EventArgs e)
??????? {
??????????? XmlDocument doc = new XmlDocument();
??????????? doc.Load(@"E:\ruxiaomeng\Simple\C++\CLR\stlclr\StlClr Sample\XMLTest\bookstore.xml");
??????????? XmlNodeList nodes = doc.SelectSingleNode("bookstore").ChildNodes; //找到根節點下的所有子節點。
?
??????????? foreach (XmlElement item in nodes)
??????????? {
??????????????? if (item.GetAttribute("genre") == "歷史")//找屬性
??????????????? {
??????????????????? item.SetAttribute("genre", "中國古代史");
??????????????? }
?
??????????????? XmlNodeList childsnodes = item.ChildNodes;
??????????????? foreach (XmlElement childitem in childsnodes)
??????????????? {
??????????????????? if (childitem.Name == "price") //找節點
??????????????????? {
??????????????????????? childitem.InnerText = "199";
??????????????????????? break;
??????????????????? }
??????????????? }
??????????? }
?????? ?????doc.Save(@"E:\ruxiaomeng\Simple\C++\CLR\stlclr\StlClr Sample\XMLTest\bookstore.xml");
??????? }
?
??????? //刪除節點的genre屬性,刪除price是99的節點
??????? protected void btn_Del_Click(object sender, EventArgs e)
??????? {
??????????? XmlDocument doc = new XmlDocument();
??????????? doc.Load(@"E:\ruxiaomeng\Simple\C++\CLR\stlclr\StlClr Sample\XMLTest\bookstore.xml");
?
??????????? XmlNode root = doc.SelectSingleNode("bookstore");
?
??????????? foreach (XmlElement item in root.ChildNodes)
??????????? {
??????? ????????if (item.HasAttribute("genre"))
??????????????? {
??????????????????? item.RemoveAttribute("genre");
??????????????? }
??????????????? foreach (XmlElement child_item in item)
??????????????? {
??????????????????? if (child_item.Name == "price" && child_item.InnerText == "99")
??????????????????? {
??????????????????????? root.RemoveChild(item);
??????????????????? }
??????????????? }
??????????? }
??????????? doc.Save(@"E:\ruxiaomeng\Simple\C++\CLR\stlclr\StlClr Sample\XMLTest\bookstore.xml");
?? ?????}
?
轉載于:https://www.cnblogs.com/hometown/p/3204229.html
總結
以上是生活随笔為你收集整理的XML简单的增改删操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 矫正视力眼镜多少钱
- 下一篇: mongoDB 入门指南、示例