日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

XML简单的增改删操作

發布時間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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简单的增改删操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。