为什么Xmlreadere花了这么多时间 [英] why Xmlreadere is taking so much of time

查看:83
本文介绍了为什么Xmlreadere花了这么多时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用xmlreader来读取xml来为我创建数据表。

从本地路径编码时,执行速度非常快。 br />
我使用下面的代码:



Hi all,
I was using xmlreader to read the xml to create datatable for me.
While coding from the local path the execution speed was very fast.
I used the code like below:

 XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;
            using (XmlReader xmlReader = XmlReader.Create(@"C:/Users/Travel Decorum/Documents/Visual Studio 2010/Projects/Traveldecorum/Traveldecorum/folderxml/XMLFile1.xml",settings))
            {
 xmlReader.ReadToDescendant("Result");
while (xmlReader.Read())
                {
 if (xmlReader.Name.Equals("AirResult") && xmlReader.NodeType==XmlNodeType.Element)
                    {
                       
                        

                        Air_Result_Id = Convert.ToString(xmlReader.GetAttribute("ResultItemID"));
                        airline_code = Convert.ToString(xmlReader.GetAttribute("Operator"));
                        a_fare = Convert.ToString(Convert.ToDouble(xmlReader.GetAttribute("AdultTotal")) + (5));
                        c_fare = Convert.ToString(Convert.ToDouble(xmlReader.GetAttribute("ChildTotal")) + (5));
                        i_fare = Convert.ToString(Convert.ToDouble(xmlReader.GetAttribute("InfantTotal")) + (5));
                        airline_name = Convert.ToString(xmlReader.GetAttribute("OperatorName"));
                        fare = Convert.ToString((Convert.ToDouble(xmlReader.GetAttribute("TotalTax"))) + (Convert.ToDouble(xmlReader.GetAttribute("TotalGross"))) + (5 * (Adult + Child + Infant)));
                       
                        //grid_table_main.Rows.Add(dr_grid_table2);
                        
                    }
}
}





但是当我更改第一行代码时它与webresponse一起开始耗费大量时间。





But when i changed the first line of code to use it with webresponse it started taking so much of time.

using (XmlReader xmlReader = XmlReader.Create(req.GetResponse().GetResponseStream(), settings))





为什么会这样正在发生。它现在开始花费10倍的时间来执行响应。

请帮助



why this is happening.It now started taking 10 times more time to execute with response.
Please help

推荐答案

GC不应该收集来自服务的流,但这并非不可能,并且肯定会解释观察到的行为。



我们可以通过明确地将服务的结果放入记忆场m。



The GC shouldn't be collecting the stream from the service, but it's not impossible and would certainly explain the observed behavior.

We might be able to fix this by explicitly putting the results of the service into a memorystream.

using(var theStream = new MemoryStream(req.GetResponse().GetResponseStream())
{
    XmlReaderSettings settings = new XmlReaderSettings { IgnoreWhitespace = true};

    using (XmlReader xmlReader = XmlReader.Create(theStream,settings))
    {
        // rest of code as before
    }
}





这消除了您身边的流可能出现的问题。如果您仍然遇到问题,还有其他两种可能的可能性:

1.普通旧延迟

2.网络服务效率低下。



This eliminates possible issues with the stream on your side. If you're still seeing issues there are 2 other likely possibilities:
1. Plain-Old Latency
2. Inefficiencies in the web service.


这篇关于为什么Xmlreadere花了这么多时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆