C# 关于 xmlreader xmldocument 和xpath 之间的性能比较
来源:互联网 发布:概预算软件破解版 编辑:程序博客网 时间:2024/06/01 07:28
下面列出微软.net类库提供的读写xml文件个类及其特点:
本文提到的XmlReader也是微软类库中的一个类,它的特点是快速高效,并且可扩展,缺点是只读。
本人关于这三个做了一个性能测试
条件:做100000次,每次都进行XML的加载和一个节点的读取,所共的时间如下
xpath: 03.265625
dom: 02.625
xmlreader: 0.140625
其中xpath和dom比较类似,大部分时间都在加载XML上,节点查找倒是挺快的,而xmlreader则是边加载边查找,不需要完全加载完才开始查找节点,可以用于大容量的XML读取,便代码编写上没有前两个快捷
DateTime dt1, dt2;
string linkId;
dt1 = DateTime.Now;
for (int i = 0; i < 100000; i++)
{
linkId = XPathTest(strXml);
}
dt2 = DateTime.Now;
Console.WriteLine("XPathTest: {0}", dt2 - dt1);
dt1 = DateTime.Now;
for (int i = 0; i < 100000; i++)
{
linkId = DOMTest(strXml);
}
dt2 = DateTime.Now;
Console.WriteLine("DOMTest: {0}", dt2 - dt1);
dt1 = DateTime.Now;
for (int i = 0; i < 100000; i++)
{
linkId = XmlReaderTest(strXml);
}
dt2 = DateTime.Now;
Console.WriteLine("XmlReaderTest: {0}", dt2 - dt1);
//XmlStream.Close();
Console.ReadLine();
#region XPathTest
static string XPathTest(string strXml)
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(strXml);
//XmlNode linkId0 = XmlDoc.SelectSingleNode("Response/SessionID");
//XmlNode linkId1 = XmlDoc.SelectSingleNode("Response/Result/Item/LastFeed");
//XmlNode linkId2 = XmlDoc.SelectSingleNode("Response/Result/Item/LandStatus");
//XmlNode linkId3 = XmlDoc.SelectSingleNode("Response/Result/Item/AnimalID");
//XmlNode linkId4 = XmlDoc.SelectSingleNode("Response/Result/Item/RaiseTime");
//XmlNode linkId5 = XmlDoc.SelectSingleNode("Response/Result/Item/AnimalName");
XmlNode linkId = XmlDoc.SelectSingleNode("Response/Result/Item/LandID");
return linkId.InnerXml;
}
#endregion
#region DOMTest
static string DOMTest(string strXml)
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(strXml);
//XmlNode linkId0 = XmlDoc["Response"]["SessionID"];
//XmlNode linkId1 = XmlDoc["Response"]["Result"]["Item"]["LastFeed"];
//XmlNode linkId2 = XmlDoc["Response"]["Result"]["Item"]["LandStatus"];
//XmlNode linkId3 = XmlDoc["Response"]["Result"]["Item"]["AnimalID"];
//XmlNode linkId4 = XmlDoc["Response"]["Result"]["Item"]["RaiseTime"];
//XmlNode linkId5 = XmlDoc["Response"]["Result"]["Item"]["AnimalName"];
XmlNode linkId = XmlDoc["Response"]["Result"]["Item"]["LandID"];
return linkId.InnerXml;
}
#endregion
#region XmlReaderTest
static string XmlReaderTest(string strXml)
{
string restr = "";
MemoryStream XmlStream = new MemoryStream();
byte[] bytes = Encoding.UTF8.GetBytes(strXml);
XmlStream.Write(bytes, 0, bytes.Length);
XmlStream.Position = 0;
XmlTextReader reader = new XmlTextReader(new StreamReader(XmlStream));
while (reader.Read() && reader.Name != "Response") ;
while (reader.Read() && reader.Name != "Result") ;
while (reader.Read() && reader.Name != "Item") ;
while (reader.Read() && reader.Name != "LandID") ;
restr = reader.ReadString();
XmlStream.Close();
return restr;
}
#endregion
- C# 关于 xmlreader xmldocument 和xpath 之间的性能比较
- XML文件的读取用XmlDocument和XmlReader的比较
- XML文件的读取用XmlDocument和XmlReader的比较
- XML文件的读取用XmlDocument和XmlReader的比较
- XmlDocument与XmlReader比较
- C#操作Xml的两种方式 XPath XmlDocument XmlNodeList
- C#操作Xml的两种方式 XPath XmlDocument XmlNodeList
- C#操作Xml的两种方式 XPath XmlDocument XmlNodeList
- C#操作Xml的两种方式 XPath XmlDocument XmlNodeList
- C#操作Xml的两种方式 XPath XmlDocument
- XML的dom,sax解析及XmlDocument和XMLTextReader原理及性能比较
- XPath和JCR-SQL2性能比较
- 使用XmlReader和XmlDocument 读取xml文件,使用总结
- 使用XMLReader、XMLDocument和DataSet读取xml文件及效率
- 在.NET中操作XmlDocument,XmlReader的方法
- C++ 和 C# 之间的比较
- 关于XmlDocument 和 XPathDocument
- C++和C#的性能比较
- (转)Sqlserver合并复制的web同步详解(转微软三篇)2
- Spring中Quartz的配置
- Sqlserver合并复制的web同步详解(转微软三篇)3
- 在国外BBS上找到的获取BDA设备信号统计值方法
- xml
- C# 关于 xmlreader xmldocument 和xpath 之间的性能比较
- ctags 使用细节
- WinCE串口编程API函数(二)
- Sun Swing学习地址
- Drupal - 模块描述文件结构
- UIIC的全面理解
- BMP文件结构
- 数字电视电子节目指南(EPG)的实现原理分析
- 微机WindowsXP中Dev-cpp中的运算程序(30000位)(C++)