---
title: "读取XML的三种方式，及其性能分析和比较（转）"
author: "传说中的旅人"
date: "2011-07-08T09:33:22Z"
tags: []
categories: ["Web前端"]
slug: "xml"
draft: false
lastmod: "2011-07-08T09:34:37Z"
id: 1119304
url: "https://lengend.iteye.com/blog/1119304"
---

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

XmlDocument xmldoc = new XmlDocument();  
xmldoc.Load(...);  
string ns = "[http://schemas.xmlsoap.org/soap/envelope/](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）：

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

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

 

原文地址：[http://blog.csdn.net/maomaochong713/article/details/419146](http://blog.csdn.net/maomaochong713/article/details/419146)