1

I know this must be a rookie question, but how do i accomplish that? Because from what i have seen here : http://msdn.microsoft.com/en-us/library/system.io.streamwriter or at XMLWriter is not helping me, because i just want to save all, not write specific lines.

Basically i have an httpRequest that returns back an XML response. I am getting that in a stream, and from that i want to save it to an xml file, for later use. Part of the code:

HttpWebResponse response = (HttpWebResponse)httpRequest.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();

            XDocument blabla = XDocument.Parse(responseString);

            // Here is where the saving to a file should occur

            streamResponse.Close();
            streamRead.Close();
H H
  • 263,252
  • 30
  • 330
  • 514
observ
  • 157
  • 2
  • 4
  • 12

3 Answers3

3

Why do you need to parse the file? In .NET 4 you can just write it directly to disk using a file stream like this:

using (var fileStream = File.Create("file.xml"))
{
    streamResponse.CopyTo(fileStream);
}

If you are using an earlier version of the .NET framework, you can use the method described here to copy data from one stream to another.

Community
  • 1
  • 1
Alex Peck
  • 4,603
  • 1
  • 33
  • 37
0

Have a look at this:

http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.save.aspx

Should help you get the job done!

Logard
  • 1,494
  • 1
  • 13
  • 27
0

As you already have the XML response as a string, I think what you need is to use a StreamWriter class to write the response string straight to a file.

There is an MSDN example of its use here: How to: Write Text to a File

KazR
  • 961
  • 7
  • 16