DataContractSerializer - 意外的EOF [英] DataContractSerializer - Unexpected EOF

查看:100
本文介绍了DataContractSerializer - 意外的EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有想要序列化的对象。我决定使用DataContractSerializer,但是我得到一个运行时错误,说当序列化程序尝试ReadObjects时我的程序遇到了未经检测的文件。下面是代码:

I have objects that I want to serialize. I decided to use DataContractSerializer, but I get a runtime error saying that my program encounters unexpexted of file when the serializer try to ReadObjects. Here is the code:

public static async Task SaveFeedsToFile(List<FeedItem> wantToBeSaved)
{
    StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    StorageFile localFile = await localFolder.CreateFileAsync("tes.xml", CreationCollisionOption.ReplaceExisting);
    using (IOutputStream writeStream = await localFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        Stream stream = writeStream.AsStreamForWrite();
        DataContractSerializer ser = new DataContractSerializer(typeof(List<FeedItem>));
        ser.WriteObject(stream, wantToBeSaved);
        await stream.FlushAsync();
    }
}

public static async Task<List<FeedItem>> LoadFeedsFromFile()
{
    List<FeedItem> r = new List<FeedItem>();
    StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    StorageFile localFile = await localFolder.CreateFileAsync("tes.xml", CreationCollisionOption.ReplaceExisting);
    using (IInputStream readStream = await localFile.OpenAsync(FileAccessMode.Read))
    {
        Stream stream = readStream.AsStreamForRead();
        DataContractSerializer ser = new DataContractSerializer(typeof(List<FeedItem>));
        r = (List<FeedItem>)ser.ReadObject(stream);
    }
    return r;
}

我的程序执行粗线时发现错误。有什么建议吗?

The error is found while my program executes the bold line. Any suggestion?

推荐答案

看起来你正在创建一个全新的文件(tes.xml),然后试图从中读出信息&NBSP;对我来说,为什么你得到EOF似乎很明显。 如果我误解,请解释。
It looks like you're creating a brand-new file (tes.xml), then trying to read out information from it.  It seems obvious to me why you're getting EOF.  If I am misunderstanding, please explain.


这篇关于DataContractSerializer - 意外的EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆