0

我正在使用 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();
}

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

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),我不知道为什么

4

1 回答 1

1

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

附带说明一下,更改save为 returnTaskawait它是一个好主意。

于 2012-12-20T17:02:57.980 回答