C# 关于 xmlreader xmldocument 和xpath 之间的性能比较

下面列出微软.net类库提供的读写xml文件个类及其特点:

类名称优点缺点
XmlReader快速、高效、可扩展只读,只向前,需要人工验证
XmlDocument可往返、可读写、支持XPath筛选比XmlReader慢
XPathNavigator可往返,支持XPath和XSLT只读
XPathDocument比XmlDocument,优化支持XPath和XSLT比XmlReader慢

本文提到的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值