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

歡迎訪問 生活随笔!

生活随笔

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

生活经验

C#用XmlDocument操作XML

發布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#用XmlDocument操作XML 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.加載xml文件

?? string xmlPath = AppDomain.CurrentDomain.BaseDirectory+"xml/test.xml";
?? XmlDocument xmlDoc = new XmlDocument();
?? xmlDoc.Load(xmlPath);//這里是xml文件的路徑

?? string xmlString="<books><book>test</book></books>";

?? xmlDoc.LoadXml(xmlString);//這里是加載一段XML字符串并將它轉換為xml

2、用xpath來查詢節點

??? XmlNodeList nodeList = xmlDoc.SelectNodes("/bookstore/book/title");//查詢節點集

??? XmlNodeList node = xmlDoc.SelectSingleNode("/bookstore/book/title[@speciality='computer']");//根據屬性查詢單個節點

3、遍歷節點集

??? foreach (XmlNode node in nodeList)
4、給某個節點添加新的屬性

??? XmlAttribute attr = xmlDoc.CreateAttribute("specialty");

??? attr.Value = "computer";
??? node.Attributes.Append(attr);

5、刪除某個節點的某個屬性,當然也可以根據屬性的下標來刪除(這里就不舉例了)

??? ?XmlNode node = xmlDoc.SelectSingleNode("/bookstore/book[@ISBN='1-861003-11-0']/title");
??????????? XmlAttribute attr = node.Attributes["specialty"];
??????????? if (attr.Value == "computer1")
??????????? {
??????????????? node.Attributes.Remove(attr);
??????????? }

6、修改某個節點的value值,但節點的tag名稱是不能修改的,只能先刪除再添加

????? node.Attributes["specialty"].Value="xiaochun";

7、添加節點

??? XmlElement element = xmlDoc.CreateElement("book");
??????????? element.InnerXml = "<test>create element</test>";
??????????? XmlNode node = xmlDoc.SelectSingleNode("/bookstore");
??????????? node.AppendChild(element);

8、修改節點就是先查詢到某個節點,然后再將他的屬性再設置一次就行了(這里就不舉例了)

9、刪除節點
???? XmlNodeList nodeList = xmlDoc.SelectNodes("/bookstore/book");
??????????? foreach (XmlNode node in nodeList)
??????????? {
??????????????? if (node.Attributes.Count == 0)//這里表示沒有屬性的節點
??????????????? {
??????????????????? xmlDoc.SelectSingleNode("/bookstore").RemoveChild(node);
??????????????? }
??????????? }

10、保存xml文檔

???? xmlDoc.Save(xmlPath);

11、創建一個完整的XML文檔

????? XmlDocument xmlDoc = new XmlDocument();
??????????? XmlDeclaration declare = xmlDoc.CreateXmlDeclaration("1.0","gb2312",null);
??????????? xmlDoc.AppendChild(declare);

??????????? XmlElement root = xmlDoc.CreateElement("books");
??????????? xmlDoc.AppendChild(root);

??????????? XmlElement bookElement = xmlDoc.CreateElement("book");
??????????? bookElement.InnerXml = "<name>javascript</name>";
??????????? xmlDoc.SelectSingleNode("/books").AppendChild(bookElement);

??????????? XmlElement bookElement2 = xmlDoc.CreateElement("book");
??????????? bookElement.InnerXml = "<name>xml</name>";
??????????? XmlAttribute attr = xmlDoc.CreateAttribute("title");
??????????? attr.Value = "test";
??????????? bookElement2.Attributes.Append(attr);
??????????? xmlDoc.SelectSingleNode("/books").AppendChild(bookElement2);

??????????? xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory+"/xml/test2.xml");

轉載于:https://www.cnblogs.com/msql/archive/2012/11/30/2796235.html

總結

以上是生活随笔為你收集整理的C#用XmlDocument操作XML的全部內容,希望文章能夠幫你解決所遇到的問題。

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