Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
 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);
                        
                    }
}
}


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

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


why this is happening.It now started taking 10 times more time to execute with response.
Please help
Posted
Updated 25-Aug-14 18:52pm
v2

1 solution

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.

C#
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
    }
}


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.
 
Share this answer
 
Comments
hrishisinha 25-Aug-14 9:30am    
@Nathan
I tried your code but no luck.
It is throwing errors(memory stream has invalid arguments)
Nathan Minier 25-Aug-14 9:32am    
Is req.GetResponse().GetResponseStream() not in byte[] format?
hrishisinha 25-Aug-14 9:34am    
i am using a webservice to get the response.
I just used ur code as per yoursuggestion but it is throwing error
Nathan Minier 25-Aug-14 9:42am    
I just typed it out without testing, just noticed the syntax error:

using(var theStream = new MemoryStream(req.GetResponse().GetResponseStream())

should be

using(var theStream = new MemoryStream(req.GetResponse().GetResponseStream()))

And if that's still an issue, try just adding as a variable to define a scope for the GC

using(var theStream = req.GetResponse().GetResponseStream())
hrishisinha 25-Aug-14 10:03am    
using(var theStream = req.GetResponse().GetResponseStream())

this is working but still the execution time is same

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900