XmlDocument与xelement性能(XMLDOCUMENT VS XELEMENT PERFORMANCE)

      我已经使用XElement有一段时间了,下面我做了一些性能的测试,并指出在xmlDocuemnt和xelement之间的不同。

首先,XElement是.NET Framework3.5的一部分,它被使用在xml to linq,并且位于System.xml.linq命名空间下。这个class在linq方面得到了很好的使用。xElement.Nodes和xElement.Attribues返回了一个IEnumerable,这个接口很容易的被使用于nodes的访问。当然你也可以使用Lambda expressions操作它们。更多的信息请参考http//msdn.microsoft.com/en-us/library/bb487098.aspx
在下面的例子中,我创建了两个static方法,一个使用xmlDocument,一个使用xElement去生成一个XML,通过循环system assembly exported type构建一个巨大的xml ,并将其保存到Stream中去,下面是两段代码:
   1://Generates XML using XmlDocument
   2: internal static void GenerateXmlUsingXmlDocument() 
   3: {
   4:     MemoryStream ms =new MemoryStream();
   5:     XmlDocument xmlDoc = new XmlDocument();
   6:     xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "no"));
   7:     XmlElement assembliesNode = xmlDoc.CreateElement("Assemblies");
   8:     foreach (Type t in Assembly.GetAssembly(typeof(Object)).GetExportedTypes())
   9:     {
  10:         XmlElement assemblyNode = xmlDoc.CreateElement("Assembly");
  11:         XmlAttribute fullTypeName = xmlDoc.CreateAttribute("FullTypeName");
  12:         fullTypeName.Value = t.ToString();
  13:         XmlAttribute isInterfaceName = xmlDoc.CreateAttribute("IsInterface");
  14:         isInterfaceName.Value = t.IsInterface.ToString();
  15:         assemblyNode.Attributes.Append(fullTypeName);
  16:         assemblyNode.Attributes.Append(isInterfaceName);
  17:         assembliesNode.AppendChild(assemblyNode);
  18:     }
  19:     xmlDoc.AppendChild(assembliesNode);
  20:     xmlDoc.WriteContentTo(new XmlTextWriter(ms,System.Text.ASCIIEncoding.ASCII));
  21: }
  22:  
  23: //Generates XML using XElement
  24: internal static void GenerateXmlUsingXElement()
  25: {
  26:     MemoryStream ms = new MemoryStream();
  27:     XElement assembliesNode = new XElement("Assemblies",
  28:             from Type t in Assembly.GetAssembly(typeof(Object)).GetExportedTypes()
  29:             select new XElement("Assembly",
  30:                 new XAttribute("FullTypeName", t.ToString()),
  31:                 new XAttribute("IsInterface", t.IsInterface.ToString())));
  32:     
  33:     assembliesNode.Save(new XmlTextWriter(ms, System.Text.ASCIIEncoding.ASCII));
  34: }
你可以发现上面两个方法中的不同了吧,xElement使用 LINQ expressions生成xml,它代码简单明了。通过下面的代码,我重复的测试在每个方法中执行时间
1: Stopwatch sw = new Stopwatch();
   2: for (int index = 0; index <>
   3: {
   4:     sw.Reset(); sw.Start();
   5:     Program.GenerateXmlUsingXmlDocument();
   6:     sw.Stop();
   7:     Console.Write("Generation time using XmlDocument " +
   8:         "and XElement: " + sw.ElapsedMilliseconds);
   9:     sw.Reset(); sw.Start();
  10:     Program.GenerateXmlUsingXElement();
  11:     sw.Stop();
  12:     Console.WriteLine(" : " + sw.ElapsedMilliseconds);
  13:     //Forcing the Garbage Collector to run to make sure,
  14:     //We dispose of all the types we created on the
  15:     //Managed heap.
  16:     GC.Collect();
  17: }
  18: Console.ReadKey();
从上面的代码中,你可以发现我循环50次去测试每一个方法,另外,在每个测试后,我都显视的调用了GC回收器去清理通过两个方法创建的对象。下面我显示测试的结果:
XmlDocumentXElement

在上面,你可以清楚的发现,在两个方法中create xml的不同。它们之间的执行时间相差了6到10倍。在ASP.NET applications 和Web Services 中以每毫秒计算犹为重要。还有一件事情提醒一下,xElement是 .NET 3.5中System.xml.linq.dll的一部分,但是它仍是以.NET 2.0 CLR运行的,因此,如果你还没有使用.NET 3.5,你可以 copy System.Xml.Linq.dll在你的应用程序下。但是,你会错过许多来自于.NET team的好的更新。

      注:转载请标明出处,谢谢!

      Blogger: http://leo-zhang.blogspot.com/

英文地址(引用):XMLDOCUMENT VS CELEMENT PERFORMANCE


posted @ 2008-10-11 14:31  译时代  阅读(2245)  评论(1编辑  收藏  举报