为什么我的文件没有保存? WinRT中的XmlSerializer

时间:2012-12-20 16:52:00

标签: xml-serialization windows-runtime storage

我正在使用XmlSerializer,每次将项目添加到我的列表中时我都会调用save方法:

async public void save(List<string> eingabe)
{
    var serializer = new XmlSerializer(typeof(List<string>));

    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    StorageFile sampleFile = 
        await storageFolder.CreateFileAsync(speicherPfad, CreationCollisionOption.ReplaceExisting);
    var file = await sampleFile.OpenAsync(FileAccessMode.ReadWrite);

    serializer.Serialize(file.AsStreamForWrite(), eingabe);

    file.Dispose();
}

当应用程序启动时,我调用load方法:

public async Task<List<string>> load()
{
    List<string> ausgabe = new List<string>();
    XmlSerializer serializer = new XmlSerializer(typeof(List<string>));

    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    StorageFile sampleFile = await storageFolder.CreateFileAsync(speicherPfad, CreationCollisionOption.ReplaceExisting);

    var file = await sampleFile.OpenAsync(FileAccessMode.Read);
    List<string> speicher = (List<string>)serializer.Deserialize(file.AsStreamForRead());

    file.Dispose();

    return speicher;
}

问题:在加载方法中,文件总是空的(大小= 0),我不知道为什么

1 个答案:

答案 0 :(得分:1)

可能是因为这个选项:CreationCollisionOption.ReplaceExisting

作为旁注,最好将save更改为返回Taskawait