C# 序列化字节数组与 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/570535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serialization byte array vs XML file
提问by
I am heavily using byte array to transfer objects, primitive data, over the network and back. I adapt java's approach, by having a type implement ISerializable, which contains two methods, as part of the interface, ReadObjectData and WriteObjectData. Any class using this interface, would write date into the byte array. Something Like that
我大量使用字节数组来通过网络和返回传输对象、原始数据。我调整了 java 的方法,通过让类型实现 ISerializable,它包含两个方法,作为接口的一部分,ReadObjectData 和 WriteObjectData。任何使用此接口的类都会将日期写入字节数组。类似的东西
class SerializationType:ISerializable
{
void ReadObjectData (/*Type that manages the write/reads into the byte array*/){}
void WriteObjectData(/*Type that manages the write/reads into the byte array*/){}
}
After write is complete for all object, I send an array of the network.
所有对象的写入完成后,我发送一个网络数组。
This is actually two-fold question. Is it a right way to send data over the network for the most efficiency (in terms of speed, size)?
这实际上是两个问题。这是通过网络发送数据以获得最高效率(在速度、大小方面)的正确方法吗?
Would you use this approach to write objects into the file, as opposed to use typically xml serialization?
您会使用这种方法将对象写入文件,而不是使用典型的 xml 序列化吗?
Edit #1
编辑 #1
Joel Coehoornmentioned BinaryFormatter. I have never used this class. Would you elaborate, provide good example, references, recommendations, current practices -- in addition to what I currently see on msdn?
Joel Coehoorn提到了 BinaryFormatter。我从来没有用过这个类。除了我目前在 msdn 上看到的内容之外,您能否详细说明、提供好的示例、参考资料、建议、当前做法?
采纳答案by Joel Coehoorn
This should be fine, but you're doing work that is already done for you. Look at the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
class.
这应该没问题,但是您正在做已经为您完成的工作。看System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
班级。
Rather than needing to implement your own Read/WriteOjbectData() methods for each specific type you can just use this class that can already handle most any object. It basically takes an exact copy of the memory representation of almost any .Net object and writes it to or reads it from a stream:
无需为每个特定类型实现自己的 Read/WriteOjbectData() 方法,您只需使用这个已经可以处理大多数对象的类。它基本上获取几乎所有 .Net 对象的内存表示的精确副本,并将其写入流或从流中读取:
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(outputStream, objectToSerialize);
objectToDeserialize = bf.Deserialize(inputStream) as DeserializedType;
Make sure you read through the linked documents: there can be issues with unicode strings, and an exact memory representation isn't always appropriate (things like open Sockets, for example).
确保您通读了链接的文档:unicode 字符串可能存在问题,并且精确的内存表示并不总是合适的(例如,诸如开放套接字之类的东西)。
回答by Jon Skeet
Creating your own ISerializable interface when there's already one in the framework sounds like a bit of a recipe for disaster. At least give it a different name.
当框架中已经存在 ISerializable 接口时,创建您自己的 ISerializable 接口听起来有点像灾难的秘诀。至少给它一个不同的名字。
You'll have a bit of a problem when it comes to reading - you won't have an instance to call the method on. You might want to make it a sort of "factory" instead:
在阅读时你会遇到一些问题——你不会有一个实例来调用这个方法。你可能想让它成为一种“工厂”:
public interface ISerializationFactory<T>
{
T ReadObjectData(Stream input);
void WriteObjectData(Stream output);
}
As for XML vs binary... it entirely depends on the situation: how much data will there be, do you need backwards and forwards compatibility, does the XML serialization in .NET give you enough control already etc.
至于 XML 与二进制……这完全取决于情况:将有多少数据,您是否需要向后和向前兼容性,.NET 中的 XML 序列化是否已经为您提供了足够的控制等。
回答by Andrew Hare
Yes this will be faster than sending XML as you will be sending less data over the wire. Even if you compressed the XML (which would drastically reduce its size) you would still have the overhead of compression and decompression. So I would say that between what you are currently doing and XML serialization you are currently using the most efficient solution.
是的,这将比发送 XML 更快,因为您将通过线路发送更少的数据。即使您压缩了 XML(这将大大减少其大小),您仍然会有压缩和解压缩的开销。所以我想说,在您目前正在做的事情和 XML 序列化之间,您目前使用的是最有效的解决方案。
However I am curious as to how much of a performance hit you would incur by using XML instead of a marshaled object. The reason that I would encourage you to look into XML serialization is because you will be storing the data in an application-neutral format that is also human readable. If you are able to serialize the data to XML in a way that does not incur performance penalties in your application I would recommend that you look into it.
但是,我很好奇使用 XML 而不是封送对象会对性能造成多大影响。我鼓励您研究 XML 序列化的原因是因为您将以与应用程序无关的格式存储数据,该格式也是人类可读的。如果您能够以不会导致应用程序性能下降的方式将数据序列化为 XML,我建议您研究一下。
回答by Welbog
Regarding writing to file, generally you want to serialize an object to XML if you want to be able to read the serialization or perhaps alter it. If you have no desire for the serialization to be human readable, you might as well reuse your binary serialization.
关于写入文件,如果您希望能够读取序列化或更改它,通常您希望将对象序列化为 XML。如果您不希望序列化成为人类可读的,那么您不妨重用二进制序列化。
If you do want it to be human readable, then XML is something to consider, but it depends on the type of data you need to serialize. XML is inherently recursive and is therefore good for serializing likewise recursive data. It's less of a good fit on other types of data.
如果您确实希望它是人类可读的,那么需要考虑 XML,但这取决于您需要序列化的数据类型。XML 本质上是递归的,因此非常适合序列化类似的递归数据。它不太适合其他类型的数据。
In other words, pick a persistent serialization that suits your needs. There's no one-way-fits-all solution here.
换句话说,选择适合您需求的持久序列化。这里没有一种万能的解决方案。
As for network, generally you'll want to keep size to a minimum, so XML is usually never a good choice due to its verbosity.
至于网络,通常您希望将大小保持在最小,因此 XML 由于其冗长,通常永远不是一个好的选择。
回答by TofuBeer
Serialization (in Java) is deceptively simple. As long as you do simple stuff (like never change the class) it is easy - but there are a number of "fun" things with it too.
序列化(在 Java 中)看似简单。只要你做一些简单的事情(比如永远不要改变课程),这很容易——但它也有很多“有趣”的东西。
Foe a good discussion on Java serialization look at Effective Java(specifically chapter 10).
关于 Java 序列化的精彩讨论参见Effective Java(特别是第 10 章)。
For C#, not sure, but likely the core issues are the same.
对于 C#,不确定,但核心问题可能是相同的。
There is an example here on C# serialization: http://www.codeproject.com/KB/cs/objserial.aspx.
这里有一个关于 C# 序列化的例子:http: //www.codeproject.com/KB/cs/objserial.aspx。
回答by 01es
XStreamlibrary provide an exceptionally good way of dealing with serialisation including support for XML, JSON and supporting custom converters. Specifically, the use of custom converters allowed us to reduce XML verbosity and to serialise strictly what is needed.
XStream库提供了一种非常好的处理序列化的方法,包括支持 XML、JSON 和支持自定义转换器。具体来说,自定义转换器的使用使我们能够减少 XML 冗长,并严格序列化所需的内容。
XStream has no requirement to declare everything as Serializable, which is very important when one utilises a third-party lib and needs to serialise an instance of a class from that lib, which is not declared as Serializable.
XStream 没有要求将所有内容都声明为 Serializable,这在使用第三方库并需要从该库中序列化一个类的实例时非常重要,该实例未声明为 Serializable。
The answer is already accepted, but for the sake of completeness of this discussion here is a link to a good comparison between different serialisation approaches/libraries:
答案已经被接受,但为了讨论的完整性,这里有一个链接,可以很好地比较不同的序列化方法/库:
http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking
http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking
The kryolibrary looks very compelling for Java serialisation. Similarly to XStream is supports custom converters.
该KRYO图书馆查找Java序列化非常引人注目。与 XStream 类似,支持自定义转换器。
回答by Marc Gravell
If you are after simple, lightweight and efficient binary serialization, consider protobuf-net; based on google's protocol buffers format, but implemented from scratch for typical .NET usage. In particular, it can be used either standalone (via protobuf-net's Serializer
), or via BinaryFormatter
by implementing ISerializable
(and delegating to Serializer
).
如果您追求简单、轻量级和高效的二进制序列化,请考虑protobuf-net;基于 google 的协议缓冲区格式,但为典型的 .NET 使用从头开始实现。特别是,它既可以独立使用(通过 protobuf-net 的Serializer
),也可以BinaryFormatter
通过实现ISerializable
(并委托给Serializer
)使用。
Apart from being efficient, this format is designed to be extensible and portable (i.e. compatible with java/php/C++ "protocol buffers" implementations), unlike BinaryFormatter that is both implementation-specific and version-intolerant. And it means you don't have to mess around writing any serialization code...
除了高效之外,这种格式还被设计为可扩展和可移植的(即与 java/php/C++“协议缓冲区”实现兼容),不像 BinaryFormatter 既特定于实现又不容忍版本。这意味着您不必编写任何序列化代码……