我知道这肯定是个新手问题,但是我该怎么做呢?因为从我在这里看到的:http://msdn.microsoft.com/en-us/library/system.io.streamwriter或XMLWriter对我没有帮助,因为我只想保存所有内容,而不是编写特定的行。
基本上,我有一个返回XML响应的httpRequest。我在一个流中获取它,然后我想将它保存到一个xml文件中,以供以后使用。代码的一部分:
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();发布于 2012-05-25 21:06:51
为什么需要解析文件?在.NET 4中,您可以使用如下所示的文件流将其直接写入磁盘:
using (var fileStream = File.Create("file.xml"))
{
streamResponse.CopyTo(fileStream);
}如果您使用的是早期版本的.NET框架,则可以使用所述的here方法将数据从一个流复制到另一个流。
发布于 2012-05-25 21:05:29
发布于 2012-05-25 21:09:46
由于您已经拥有了字符串形式的XML响应,因此我认为您需要使用StreamWriter类将响应字符串直接写入文件。
这里有一个使用它的MSDN示例:How to: Write Text to a File
https://stackoverflow.com/questions/10754884
复制相似问题