xmlvc++

来源:互联网 发布:淘宝女款冬季休闲裤 编辑:程序博客网 时间:2024/06/05 15:25

http://topic.csdn.net/t/20050718/16/4152283.html 

今天在csdn看到这个帖子:这段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();
} //main结束

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

 

 

 

http://download.csdn.net/detail/My_lolo/2533823