如何将流写入独立存储中的 xml 文件

本文关键字:存储 xml 文件 独立 | 更新日期: 2023-09-27 18:34:37

我正在尝试在我的应用程序中实施SkyDrive备份。 一切都运行良好,除了我不知道如何轻松地将流(从SkyDrive下载的备份(保存到隔离的Storarage中。

我知道我可以去腈化溪流,而不是保存它,但它会不必要地复杂化。

所以,这是我的问题:

我有一个包含来自SkyDrive的文件的流,我需要将其作为文件"设置.XML保存到隔离存储中。

所以基本上我需要将"流"写入独立存储中的设置.xml文件

        static void client_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e, string saveToPath)
    {
        Stream stream = e.Result; //Need to write this into IS
        try
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(saveToPath, FileMode.Create))
                {
                    //How to save it?
                }
            }
        }...

谢谢!

如何将流写入独立存储中的 xml 文件

使用 steam 的 CopyTo(( 方法 - http://msdn.microsoft.com/en-us/library/dd782932.aspx

using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(saveToPath, FileMode.Create))    
{    
  stream.CopyTo(fileStream);
} 

最快和最简单的方法是将字节从一个流逐块复制到下一个流 - 请参阅如何将一个流的内容复制到另一个流中的接受答案?


但是,如果网络流有可能损坏或包含无效数据,则可能需要在保存之前反序列化流并进行验证。