如何在Windows Phone 8.1上存储/保存Class列表?

时间:2015-11-26 10:56:17

标签: c# linq windows-phone-8.1

我有一个小项目。我想编写一个存储项目列表的Windows Phone应用程序。 我试图使用Linq将列表保存为XML文档,这在PC上运行良好,但我遇到了问题

XDoc.Save();

在PC上我可以使用String但在Windows Phone上我需要一个Stream或一个XML(Text)Writer,但我不知道如何才能做到这一点。 我也尝试保存.txt文件,我总是得到相同的异常并且不知道为什么。

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
        UnhandledException += (sender, e) =>
        {
            if (global::System.Diagnostics.Debugger.IsAttached)global::System.Diagnostics.Debugger.Break();
        };
#endif
        }
    }
}

如果有人知道存储List项的方法。请帮助我。

1 个答案:

答案 0 :(得分:0)

要保存到Windows Phone 8.1下的文件,您必须使用StorageFile对象。这是一个例子:

      using System.IO;  // needed for OpenStreamForWriteAsync()
      ....

      StorageFile storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("myfile.xml", CreationCollisionOption.ReplaceExisting);
      using (Stream stream = await storageFile.OpenStreamForWriteAsync())
      {
          //try XDoc.Save(stream);
      }

读取文件:

        StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(FILENAME);
        using (Stream stream = await storageFile.OpenStreamForReadAsync())
        {
            var XDoc = System.Xml.Linq.XDocument.Load(stream); 
        }