0

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

Ali Hasan
  • 1,045
  • 4
  • 16
  • 43

1 Answers1

2

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.

MichaelT
  • 7,574
  • 8
  • 34
  • 47
  • 1
    Regex will solve a problem of string duplication if you write it correctly but it can be slower in evaluating the string, try to measure it – MichaelT Oct 09 '12 at 09:39