title: How to overcome OutOfMemoryException pulling large xml documents from an API?
body: <p>I am pulling 1M+ records from an API. The pull works ok, but I'm getting an out of memory exception when attempting to <code>ReadToEnd</code> into a string variable.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>        XDocument xmlDoc = new XDocument();
  
          HttpWebRequest client = (HttpWebRequest)WebRequest.Create(uri);
          client.Timeout = 2100000;//35 minutes
          WebResponse apiResponse = client.GetResponse();
  
          Stream receivedStream = apiResponse.GetResponseStream();
          StreamReader reader = new StreamReader(receivedStream);
  
          string s = reader.ReadToEnd();
  </code></pre>
  
  <p>Stack trace:</p>
  
  <pre><code>at System.Text.StringBuilder.ToString()
  at System.IO.StreamReader.ReadToEnd()
  at MyApplication.DataBuilder.getDataFromAPICall(String uri) in
      c:\Users\RDESLONDE\Documents\Projects\MyApplication\MyApplication\DataBuilder.cs:line 578
  at MyApplication.DataBuilder.GetDataFromAPIAsXDoc(String uri) in
  c:\Users\RDESLONDE\Documents\Projects\MyApplication\MyApplication\DataBuilder.cs:line 543
  </code></pre>
  
  <p>What can I do to work around this?</p>