Ali Hasan
Ali Hasan

Reputation: 1075

c# ReadToEnd slow performance with large xml files

I get huge xml file from a service, and Im facing some performance issue with readtoend which is taking about 2 minutes to complete with 3 replace() and 1.3 minutes without using replace().

 HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
  using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd().Replace("\n", "").Replace("\r", "").Replace("\t", "");   
         sr.Close();
        //ReadToEnd it is taking about 2 minutes to complete 
}

What to use to overcome this performance issue.

Edited The xml file size is 2.77 MB

Upvotes: 0

Views: 840

Answers (1)

MichaelT
MichaelT

Reputation: 7934

When you use .Replace("\n", "") you actually copy entire string , in this case you doing it 3 times , a better approach would be to read this into String builder and making replace there, you can also reading and adding chars from string builder one char at a time and skipping the ones you don't need.

Upvotes: 2

Related Questions