基于.net3.5以上XML操作.
生活随笔
收集整理的這篇文章主要介紹了
基于.net3.5以上XML操作.
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//創建xml
View Code 1 View Code 2 [TestClass] 3 public class UnitTest1 4 { 5 static string xmlPath = "app.xml"; 6 7 [TestMethod] 8 public void CreateXML() 9 { 10 XNamespace ns = "http://www.so12.com/App";//命名空間 11 XDocument doc = new XDocument(new XDeclaration("1.0","utf-8","yes"), 12 new XComment("This is a commnet.Just input."), 13 new XElement(ns+"APP", 14 new XElement("DataBase", 15 new XElement("server","192.168.1.100",new XAttribute("desc","connning address")), 16 new XElement("baseName","report",new XAttribute("desc","database name")), 17 new XElement("loginName","sa",new XAttribute("desc","database login name")), 18 new XElement("loginPwd","sa",new XAttribute("desc","database login password"))), 19 new XElement("Software", 20 new XElement("output","C:\\Output\\"), 21 new XElement("input","C:\\Input\\"), 22 new XElement("import","C:\\Import\\")) 23 )); 24 doc.Save(xmlPath,SaveOptions.None); 25 } 26 }?
//查詢,修改,添加 XML
View Code 1 XElement xe = XElement.Load(xmlPath); 2 // XElement xe=XElement.Parse("字符串XML"); 3 IEnumerable<XElement> list = xe.Descendants("server"); 4 //list.ToList()[0].Value 取值 5 System.Diagnostics.Trace.WriteLine(list.ToList()[0].Value); 6 7 //添加 8 IEnumerable<XElement> list = xe.Descendants("DataBase"); 9 list.ToList()[0].Add(new XElement("descption", "new new node")); 10 11 //更改 12 IEnumerable<XElement> list = xe.Descendants("descption"); 13 list.ToList()[0].SetValue("next again!");?//刪除XML
View Code 1 IEnumerable<XElement> list = xe.Descendants("descption"); 2 list.ToList()[0].Remove();?
轉載于:https://www.cnblogs.com/xmyy/articles/3053986.html
總結
以上是生活随笔為你收集整理的基于.net3.5以上XML操作.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: set uid,set gid,stic
- 下一篇: jquery eaayui 学习(三)t