zoukankan      html  css  js  c++  java
  • LINQ to XML概述

      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

  • 相关阅读:
    【题解】NOIP2016换教室
    【题解】平面最近点对(加强版)
    [atcoder002E] Candy Piles [博弈论]
    [AGC002D] Stamp Rally [并查集+整体二分]
    [ACG001E] BBQ hard [dp]
    [BJOI2006][bzoj1001] 狼抓兔子 [最小割]
    [usaco jan 09] 安全路径 travel [最短路径树]
    [usaco jan 09] 气象牛 baric [dp]
    [poj1741] tree [点分治]
    [NOI2009] 植物大战僵尸 [网络流]
  • 原文地址:https://www.cnblogs.com/wangcongsuibi/p/8890370.html
Copyright © 2011-2022 走看看