StreamReader OutOfMemoryException

1.9k views Asked by At

I am using a web service that returns xml an I want to read it into a string. I am using ReadToEnd method of StreamReader class but at this moment OutOfMemory Exception occurs because of the large amount of xml data. Is there any way to get this .My code is below

        Stream dataStream;
        WebResponse response;
        string responseFromServer = string.Empty;
        string url = myurl;

        WebRequest request = WebRequest.Create(url);

        ASCIIEncoding encoding = new ASCIIEncoding();

        byte[] data = encoding.GetBytes(postData.ToString());

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        dataStream = request.GetRequestStream();
        dataStream.Write(data, 0, data.Length);
        dataStream.Close();
        response = request.GetResponse();

       using (Stream stream = response.GetResponseStream())
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                try
                {
                    responseFromServer = reader.ReadToEnd();
                }
                catch (Exception ex)
                {
                  throw ex;         
                }             
            }
        }
1

There are 1 answers

0
Jim Mischel On

There is a Load overload that takes a TextReader. StreamReader derives from TextReader, so you should be able to write doc.Load(reader).