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

posted on 2013-12-04 16:59  谭一丹  阅读(733)  评论(0编辑  收藏  举报

导航