标签:creat 序列 ext text tee void inner author append
LINQ to XML提供使用LINQ在内存中操作XML数据的编程接口,提供比DOM更简洁的开发接口。LINQ还可以对内存中的XML数据进行查询或者更改。和文档对象模型(DOM)一样。LINQ
to XML也是将XML至于内存之中,可以查询修改还可以将其另存为文件,也可以序列化后通过网络发送。
DOM模型中通过XMLDocument 来创建XML树,LINQ to XML 中通过XElemnt来创建XML树,看一下比较
static void CreateXMLDocByDom() { XmlDocument doc = new XmlDocument();//创建XML文档 XmlElement bookList = doc.CreateElement("BookList");//创建booklist根节点 XmlElement book, auth; book = doc.CreateElement("BOOK"); book.SetAttribute("Name", "Book-1"); auth = doc.CreateElement("Author"); auth.InnerText = "Author-1"; book.AppendChild(auth);//将auth添加到book节点里 bookList.AppendChild(book); book = doc.CreateElement("BOOK"); book.SetAttribute("Name", "Book-2"); auth = doc.CreateElement("Author"); auth.InnerText = "Author-2"; book.AppendChild(auth); bookList.AppendChild(book); doc.AppendChild(bookList); doc.Save("F:\\XML\\f1.XML"); }
LINQ to XML
static void CreateXMLDocByLinq(){ XElement booklist = new XElement("Booklist", new XElement [] { new XElement("BOOK", new object[]{ new XAttribute("Name","BOOK-1"), new XElement("Author","Author-1")}), new XElement("BOOK", new object[]{ new XAttribute("Name","BOOK-2"), new XElement("Author","Author-2")}) }); booklist.Save("F:\\XML\\f2.XML"); }
接下来开始学习Linq to XML
标签:creat 序列 ext text tee void inner author append
原文地址:https://www.cnblogs.com/wangcongsuibi/p/8890370.html