zoukankan      html  css  js  c++  java
  • Linq To Xml

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace LinqDemo
    {
      public   class Book
        {
          public string Title;
          public string Author;
          public DateTime Year;
          public Book(string author, string title, DateTime year)
          {
              Title = title;
              Author = author;
              Year = year;
          }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Xml;
    using System.Diagnostics;

    namespace LinqDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stopwatch sw = new Stopwatch();
                for (int i = 0; i < 5;i++ )
                {
                    sw.Reset();
                    sw.Start();
                    LinqtoXml();
                    sw.Stop();
                    Console.WriteLine("");
                    Console.WriteLine("the time of XElement execute:{0}", sw.ElapsedMilliseconds);
                    sw.Reset();
                    sw.Start();
                    XmlGeneral();
                    sw.Stop();
                    Console.WriteLine("");
                    Console.WriteLine("the time of XmlDocument execute:{0}", sw.ElapsedMilliseconds);
                    GC.Collect();
                }
                Console.Read();
            
            }
            public static void LinqtoXml()
            {
                Book[] books = new Book[]{
                             new Book("danche","世界是平的",Convert.ToDateTime("2013-3-1")),
                             new Book("谭一丹","今生缘",Convert.ToDateTime("2013-4-12"))
                             };
                XElement xm = new XElement("books",
                    from book in books               
                    select new XElement("book",
                       new XAttribute("author",book.Author),
                       new XElement("title",book.Title),
                       new XElement("year",book.Year)
                        )
                     );
                Console.Write(xm);
            }
            public static void XmlGeneral()
            {
                Book[] books = new Book[]{
                             new Book("danche","世界是平的",Convert.ToDateTime("2013-3-1")),
                             new Book("谭一丹","今生缘",Convert.ToDateTime("2013-4-12"))
                             };

                XmlDocument xdoc = new XmlDocument();
                XmlElement xeml = xdoc.CreateElement("books");
                foreach(Book book in books)
                {
                    XmlElement xmlbook = xdoc.CreateElement("book");
                    xmlbook.SetAttribute("author",book.Author);
                    XmlElement xmltitle = xdoc.CreateElement("title");
                    xmltitle.InnerText = book.Title;
                    XmlElement xmlyear = xdoc.CreateElement("year");
                    xmlyear.InnerText = book.Year.ToString();
                    xmlbook.AppendChild(xmlyear);
                    xmlbook.AppendChild(xmltitle);
                    xeml.AppendChild(xmlbook);
                }
                xdoc.AppendChild(xeml);
                xdoc.Save(Console.Out);
               
            }
        }
    }

    测试结果:

    第一次输出:

     <books>
      <book author="danche">
        <title>世界是平的</title>
        <year>2013-03-01T00:00:00</year>
      </book>
      <book author="谭一丹">
        <title>今生缘</title>
        <year>2013-04-12T00:00:00</year>
      </book>
    </books>
    the time of XElement execute:20
    <?xml version="1.0" encoding="gb2312"?>
    <books>
      <book author="danche">
        <year>2013-3-1 0:00:00</year>
        <title>世界是平的</title>
      </book>
      <book author="谭一丹">
        <year>2013-4-12 0:00:00</year>
        <title>今生缘</title>
      </book>
    </books>
    the time of XmlDocument execute:8

    第二次输出:


    <books>
      <book author="danche">
        <title>世界是平的</title>
        <year>2013-03-01T00:00:00</year>
      </book>
      <book author="谭一丹">
        <title>今生缘</title>
        <year>2013-04-12T00:00:00</year>
      </book>
    </books>
    the time of XElement execute:220
    <?xml version="1.0" encoding="gb2312"?>
    <books>
      <book author="danche">
        <year>2013-3-1 0:00:00</year>
        <title>世界是平的</title>
      </book>
      <book author="谭一丹">
        <year>2013-4-12 0:00:00</year>
        <title>今生缘</title>
      </book>
    </books>
    the time of XmlDocument execute:246

    第三次输出:


    <books>
      <book author="danche">
        <title>世界是平的</title>
        <year>2013-03-01T00:00:00</year>
      </book>
      <book author="谭一丹">
        <title>今生缘</title>
        <year>2013-04-12T00:00:00</year>
      </book>
    </books>
    the time of XElement execute:220
    <?xml version="1.0" encoding="gb2312"?>
    <books>
      <book author="danche">
        <year>2013-3-1 0:00:00</year>
        <title>世界是平的</title>
      </book>
      <book author="谭一丹">
        <year>2013-4-12 0:00:00</year>
        <title>今生缘</title>
      </book>
    </books>
    the time of XmlDocument execute:253

    第四次输出:
    <books>
      <book author="danche">
        <title>世界是平的</title>
        <year>2013-03-01T00:00:00</year>
      </book>
      <book author="谭一丹">
        <title>今生缘</title>
        <year>2013-04-12T00:00:00</year>
      </book>
    </books>
    the time of XElement execute:234
    <?xml version="1.0" encoding="gb2312"?>
    <books>
      <book author="danche">
        <year>2013-3-1 0:00:00</year>
        <title>世界是平的</title>
      </book>
      <book author="谭一丹">
        <year>2013-4-12 0:00:00</year>
        <title>今生缘</title>
      </book>
    </books>
    the time of XmlDocument execute:244


    <books>
      <book author="danche">
        <title>世界是平的</title>
        <year>2013-03-01T00:00:00</year>
      </book>
      <book author="谭一丹">
        <title>今生缘</title>
        <year>2013-04-12T00:00:00</year>
      </book>
    </books>
    the time of XElement execute:219
    <?xml version="1.0" encoding="gb2312"?>
    <books>
      <book author="danche">
        <year>2013-3-1 0:00:00</year>
        <title>世界是平的</title>
      </book>
      <book author="谭一丹">
        <year>2013-4-12 0:00:00</year>
        <title>今生缘</title>
      </book>
    </books>
    the time of XmlDocument execute:249

    从以上测试代码可以看出这两种操作xml的方法的执行效率的不同。

  • 相关阅读:
    c++中ctype常用函数总结(isprint isblank..)
    c++的const总结(转)
    c++重载输入输出运算符
    c++中的友元重载
    c++函数模板二栈实现
    c++函数模板1
    c++中IO输入输出流总结<二>
    c++中IO输入输出流总结<一>
    四层与七层得区别(转)
    ORACLE操作
  • 原文地址:https://www.cnblogs.com/flyaway100/p/3457943.html
Copyright © 2011-2022 走看看