本文介绍了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?
推荐答案
这篇关于DataContractSerializer - 意外的EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!