读取XML的三种方式,及其性能分析和比较(转)

我选择了我认为最方便而高效的方式,用DOM的方式解析,就像这样:

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(...);
string ns = "http://schemas.xmlsoap.org/soap/envelope/";
XmlNode envelope = xmldoc["Envelope",ns];
XmlNode body = envelope["Body",ns];
XmlNode req = body["SyncOrderRelationReq"];
XmlNode linkId = req["LinkID"];
Console.WriteLine(linkId.InnerXml);

有朋友认为用XPath更好。这个我也知道,在XML的整个体系中,XPath是目前查询XML的专用语言,在这种情况下,自然最“标准”。

不过我认为由于XPath多了一个解析XPath表达式的过程,会比较慢,所以用DOM的方式更好。于是写了个性能测试代码,包含DOM, XPath, XmlReader三者的对比,以作证明:

class MainClass
{
 static XmlDocument XmlDoc;
 static XmlNamespaceManager NM;
 static string NS = "http://schemas.xmlsoap.org/soap/envelope/";
 static MemoryStream XmlStream;
 [STAThread]
 static void Main(string[] args)
 {
  XmlStream = new MemoryStream();
  FileStream fs = new FileStream("C:/test.xml",FileMode.Open);
  byte[] bytes = new byte[fs.Length];
  fs.Read(bytes,0,bytes.Length);
  XmlStream.Write(bytes,0,bytes.Length);
  fs.Close();
  XmlDoc = new XmlDocument();
  XmlDoc.Load("C:/test.xml");
  NM = new XmlNamespaceManager(XmlDoc.NameTable);
  NM.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
  NM.AddNamespace("Req", "http://www.monternet.com/dsmp/schemas/");
  DateTime dt1,dt2;
  string linkId;
  dt1 = DateTime.Now;
  for(int i = 0;i < 100000;i++)
  {
   linkId = XPathTest();
  }
  dt2 = DateTime.Now;
  Console.WriteLine("XPathTest: {0}",dt2 - dt1);
  dt1 = DateTime.Now;
  for(int i = 0;i < 100000;i++)
  {
   linkId = DOMTest();
  }
  dt2 = DateTime.Now;
  Console.WriteLine("DOMTest: {0}",dt2 - dt1);
  dt1 = DateTime.Now;
  for(int i = 0;i < 100000;i++)
  {
   linkId = XmlReaderTest();
  }
  dt2 = DateTime.Now;
  Console.WriteLine("XmlReaderTest: {0}",dt2 - dt1);
  XmlStream.Close();
  Console.ReadLine();
 }
 static string XPathTest()
 {
  XmlNode linkId = XmlDoc.SelectSingleNode("/SOAP-ENV:Envelope/SOAP-ENV:Body/Req:SyncOrderRelationReq/Req:LinkID", NM);
  return linkId.InnerXml;
 }
 static string DOMTest()
 {
  XmlNode linkId = XmlDoc["Envelope",NS]["Body",NS]["SyncOrderRelationReq"]["LinkID"];
  return linkId.InnerXml;
 }
 static string XmlReaderTest()
 {
  XmlStream.Position = 0;
  XmlTextReader reader = new XmlTextReader(new StreamReader(XmlStream));
  while(reader.Read() && reader.Name != "SOAP-ENV:Envelope");
  while(reader.Read() && reader.Name != "SOAP-ENV:Body");
  while(reader.Read() && reader.Name != "SyncOrderRelationReq");
  while(reader.Read() && reader.Name != "LinkID");
  return reader.ReadString();
 }
}
 

这个XML文件还包含了名称空间,在比较“正规”的情况下,确实是这种情况。
测试结果是(K8 2800+ 64bit, 512M DDR400):

XPathTest: 00:00:02.8281250
DOMTest: 00:00:00.3750000
XmlReaderTest: 00:00:13.5156250

和我的预期相符,XPath远远慢于DOM的方式,几乎差了一个数量级。

有意思的是,用XmlReader的方式按理说最快,结果这里变成了最慢。实际上,对于最后一个测试,这里有点不公平,因为XmlDocument.Load的时候已经对XML进行了遍历,然后就建立了缓存。而XmlReader每次都需要这种Load的过程,并且没有缓存。如果把加载XML的时间考虑在内,XmlReader才是最快的。另外一个方面,由于它是一种只进只读的方式,使用XmlReader可以节省内存;而对于特别大的XML文件,比如达到数百M,XmlReader就成了唯一可行的方案。

-----

update on 4/14/2005:
今天我感觉到把DOM方式和XPath方式区分开,这个名字不太好,因为用XPath查询时,本质上也是用DOM的。所以上面第二个测试,DOMTest,应该称之为手动在DOM导航的测试,而XPathTest,相当于提供一个命令,像SQL语句那样动态编译后,自动在DOM导航。

update on 4/19/2005:
处理XML里面的名称空间确实是个很麻烦的问题,在上面的例子里面,使用了XmlNamespaceManager类来处理。而在XSL/T里面,又有个小技巧(还是基于那个XML):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:REQ="http://www.monternet.com/dsmp/schemas/">
 <xsl:output method="xml" />
 <xsl:template match="/">
  <xsl:value-of select="SOAP-ENV:Envelope/SOAP-ENV:Body/REQ:SyncOrderRelationReq/REQ:LinkID" />
 </xsl:template>
</xsl:stylesheet>

注意REQ这个名称空间的简写是我自己定义的,原来的XML文件没有

 

原文地址:http://blog.csdn.net/maomaochong713/article/details/419146

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值