I have a code to retrieve an XML file from the site, so I do as follows:
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/xml";
using (WebResponse webResponse = request.GetResponse())
{
using (Stream responseStream = webResponse.GetResponseStream())
{
if (responseStream != null)
{
reportXML = XDocument.Load(responseStream.ToString());
}
}
using (XmlWriter writer = XmlWriter.Create(@"C:\Retrieved.xml"))
{
reportXML.Save(writer);
}
}
But I debugged the app and it seems I lose everything on this line of code, so I have nothing anymore to save.
Stream responseStream = webResponse.GetResponseStream())
Any idea why it could be?
If I investigate the object I see this:
Length = ‘responseStream.Length’ threw an exception of type
‘System.NotSupportedException’
And the error is following:
Could not find file ‘C:\Documents and Settings\user\My
Documents\Visual Studio
2010\Projects\Proto\App\bin\Debug\System.Net.ConnectStream’.
This is the problem:
Calling
ToString()on a stream like that is pretty much never going to give you anything useful. If you just want to load the contents of the stream, get rid of theToStringcall:Alternatively, if you really just want to save it to disk, simply use
Stream.CopyToand don’t bother parsing it as XML in the first place.