SerializationException 未处理:输入流不是有效的二进制格式。起始内容(以字节为单位)是
真的很困难,任何帮助和/或评论将不胜感激!
我编写了一个数据库程序,需要能够将文件中的内容加载到列表中。所以基本上我正在尝试为此使用序列化和反序列化。发生错误的区域以粗体和斜体显示:
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 ...
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;
}
}
}
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 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近在其他地方看到过此报道,但我也无法在那里找到解释。。所提供的代码看起来应该没问题(尽管它会从一些
using
语句中受益匪浅,但它们不会破坏成功案例,因为您正在调用 <代码>.Close())。然而!我还要警告说,IMO
BinaryFormatter
不是在数据库中存储的一个不错的选择,因为这表明最好将其读回< em>在未来。事实上,BinaryFormatter
是类型相关的,这使得它在您对应用程序进行版本控制时非常脆弱。以下任意操作:创建新的应用程序版本、重命名/添加/删除字段、将属性更改为自动实现的属性、更改 .NET 版本、更改平台...可能使您的数据不可读,或者只能通过添加大量自定义活页夹代码才能读取。我强烈建议您考虑使用基于契约的序列化器而不是
BinaryFormatter
;以下任意一种:DataContractSerializer
(但不是NetDataContractSerializer
)、XmlSerializer
、JavascriptSerializer
、JSON.Net。如果您想要二进制文件的大小和性能,那么协议缓冲区(具有多种 C# 实现,包括 protobuf-net)被设计为(由 Google)具有版本容错性、小且快。由于该列表也是跨平台的,因此如果您将平台切换到 Java、Mono、WinRT(新的 Windows 8 子系统)、PHP 或其他任何平台,这也意味着您的数据是安全的。BinaryFormatter
将不适用于其中任何一个。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()
).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 thatBinaryFormatter
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.I strongly suggest that you consider using a contract-based serializer instead of
BinaryFormatter
; any of:DataContractSerializer
(but notNetDataContractSerializer
),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.这成功了吗?
太大了,无法发表评论
Does this succeed?
Too big for a comment