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的方法的执行效率的不同。

  • 相关阅读:
    win2008R2超过激活期,但是一个小时之后就会自动强制关机的问题
    SQL数据库中出现大量用户登录错误怎么解决
    VMware上安装centos 6.3 不能联网问题
    最简单的正则表达式
    双向认证SSL原理
    【虚拟机】在VMware中安装Server2008之后配置网络连接的几种方式
    win7 windows server 2008R2下 https SSL证书安装的搭配(搭配https ssl本地测试环境)
    使用word优雅的添加代码并上传到博客园(附:批量取消图片超链接方法)
    IIS服务器多域证书使用443端口解决方案
    服务器证书安装配置指南(IIS7)
  • 原文地址:https://www.cnblogs.com/flyaway100/p/3457943.html
Copyright © 2011-2022 走看看