未处理SerializationException:输入流不是有效的二进制格式。起始内容(以字节为单位)是 [英] SerializationException was unhandled: The input stream is not a valid binary format. The starting contents (in bytes) are

查看:656
本文介绍了未处理SerializationException:输入流不是有效的二进制格式。起始内容(以字节为单位)是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的卡住了,任何帮助和/或评论都将不胜感激!

really stuck, any help and/or comments would be greatly appreciated!

我编写了一个数据库程序,该程序需要能够从文件中加载内容进入列表。因此,基本上,我正在尝试为此使用序列化和反序列化。发生错误的区域以粗体显示并以斜体显示:

I've written a database program that needs to be able to load contents from a file into a list. So basically i'm trying to use serialization and deserialization for this. The area where the error occurs is in bold and is italicized:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    class TheFile
    {
        //Version 1 serialiser, instance specific, so a bit of a pain

        public void SerializeObject(string filename, TheDocument document)
        {
            Stream stream = File.Open(filename, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, document);
            stream.Close();
        }
        public TheDocument DeSerializeObject(string filename)
        {
            TheDocument document;
            Stream stream = File.Open(filename, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            ***document = (TheDocument)bFormatter.Deserialize(stream);***
            stream.Close();
            return document;
        }
    }
}

我收到的错误是如下所示:输入流不是有效的二进制格式。起始内容(以字节为单位)为:31-37-2F-30-39-2F-32-30-31-31-20-31-31-3A-30-36-3A ...

The error which i receive is as follows: The input stream is not a valid binary format. The starting contents (in bytes) are: 31-37-2F-30-39-2F-32-30-31-31-20-31-31-3A-30-36-3A ...

推荐答案

我最近在其他地方看到过此报告,但在 中都找不到解释。呈现的代码看起来应该 很好(尽管使用 语句会大大受益,但它们不会破坏成功因为您正在调用 .Close())。

I have seen this reported somewhere else recently, and I was unable to find an explanation there either. The code as presented looks like it should be fine (although it would benefit greatly from a few using statements, but they won't break the success case since you are calling .Close()).

但是!我还要警告,IMO BinaryFormatter 的一个很好的选择,因为它表明它是存储在数据库中的好方法希望将来再读回它们。 BinaryFormatter 依赖于类型的事实使您在对应用程序进行版本控制时非常脆弱。以下任何一项:创建新的应用程序版本,重命名/添加/删除字段,将属性更改为自动实现的属性,更改.NET版本,更改平台,... 可以

However! I would also warn that IMO BinaryFormatter is not a good choice for storage in a database, since that suggests it is desirable to read it back in the future. The fact that BinaryFormatter is type-dependent makes it very very brittle as you version your application. Any of: creating a new app-version, renaming/adding/removing a field, changing a property to an automatically implemented property, changing .NET version, changing platform, ... could make your data either unreadable, or readable only by adding a lot of custom binder code.

强烈建议您考虑使用基于合同的序列化器,而不是可读性。 BinaryFormatter ; DataContractSerializer (但不是 NetDataContractSerializer ), XmlSerializer 中的任何一个, JavascriptSerializer ,JSON.Net。如果您想要二进制文件的大小和性能,那么(由Google设计)协议缓冲区(具有多个C#实现,包括protobuf-net)是可忍受版本的,小型的和快速的。由于该列表也是跨平台的,因此如果您将平台切换到Java,Mono,WinRT(新的Windows 8子系统),PHP或其他任何方式,这也意味着您的数据是安全的。 BinaryFormatter 不能在其中任何一个上工作。

I strongly suggest that you consider using a contract-based serializer instead of BinaryFormatter; any of: DataContractSerializer (but not NetDataContractSerializer), XmlSerializer, JavascriptSerializer, JSON.Net. If you want binary for size and performance, then protocol buffers (with several C# implementations, including protobuf-net) is designed (by Google) to be version tolerant, small, and fast. Since that list is also cross-platform, it also means your data is safe if, say, you switch platform to Java, Mono, WinRT (the new windows 8 subsystem), PHP, or anything else. BinaryFormatter will not work on any of those.

这篇关于未处理SerializationException:输入流不是有效的二进制格式。起始内容(以字节为单位)是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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