读取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三者的对比,以作证明:
<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
?