什么是XmlSerializer的和BinaryFormatter的之间的差异 [英] What are the differences between the XmlSerializer and BinaryFormatter

查看:493
本文介绍了什么是XmlSerializer的和BinaryFormatter的之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间相当一部分上周工作的序列化。在那段时间里,我发现利用无论是BinaryFormatter的或XmlSerializer的例子​​很多。不幸的是,我没有找到什么是任何例子COM prehensively详细说明两者之间的差异。

I spent a good portion of time last week working on serialization. During that time I found many examples utilizing either the BinaryFormatter or XmlSerializer. Unfortunately, what I did not find were any examples comprehensively detailing the differences between the two.

我好奇的起源在于为什么BinaryFormatter的是能够直接反序列化到一个接口,而XmlSerializer的是没有的。 乔恩斯基特在回答<一个href="http://stackoverflow.com/questions/619197/casting-to-multiple-unknown-types-at-runtime/619210#619210">casting到多个(未知类型)在运行时的提供的直接二进制序列为例来的接口。 斯坦R. 为我提供了实现使用XmlSerializer我的目标在他回答的意思是<一href="http://stackoverflow.com/questions/1145791/xml-object-deserialization-to-interface/1145800#1145800">XML反序列化对象在接口上。

The genesis of my curiosity lies in why the BinaryFormatter is able to deserialize directly to an interface whilst the XmlSerializer is not. Jon Skeet in an answer to "casting to multiple (unknown types) at runtime" provides an example of direct binary serialization to an interface. Stan R. provided me with the means of accomplishing my goal using the XmlSerializer in his answer to "XML Object Deserialization to Interface."

除了明显的BinaryFormatter的使​​用二进制序列化,而XmlSerializer的使用XML我想更全面地了解了根本性的分歧。当使用一个或另一个与利弊每。

Beyond the obvious of the BinaryFormatter utilizes binary serialization whilst the XmlSerializer uses XML I'd like to more fully understand the fundamental differences. When to use one or the other and the pros and cons of each.

推荐答案

原因二进制格式能够直接反序列化到一个接口类型是因为,当一个对象是原序列包含类型和程序集信息的二进制流元数据卡在与对象数据。这意味着,当二进制格式反序列化它知道它的类型的对象,建立正确的对象,然后你可以强制转换成该对象实现了接口类型。

The reason a binary formatter is able to deserialize directly to an interface type is because when an object is originally serialized to a binary stream metadata containing type and assembly information is stuck in with the object data. This means that when the binary formatter deserializes the object it knows its type, builds the correct object and you can then cast that to an interface type that object implements.

在otherhand的XML序列化序列化只是一个模式,只序列化公共字段和对象的值,并没有类型的信息其他然后,(如接口的类型实现)。

The XML serializer on the otherhand just serializes to a schema and only serializes the public fields and values of the object and no type information other then that (e.g. interfaces the type implements).

下面是一个不错的职位,<一个href="http://www.dotneat.net/2009/03/22/NETSerializationUsingBinaryFormaterSoapFormatterAndXmlSerializer.aspx"相对=nofollow>。NET序列化,比较<一href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx"相对=nofollow> BinaryFormatter的,<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapformatter.aspx"相对=nofollow> SoapFormatter 和 XmlSerializer的的。我建议你​​看看下面的表,除了pviously提到串行的$ P $包括<一href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx"相对=nofollow>的DataContractSerializer ,<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.netdatacontractserializer.aspx"相对=nofollow> NetDataContractSerializer 和 protobuf网

Here is a good post, .NET Serialization, comparing the BinaryFormatter, SoapFormatter, and XmlSerializer. I recommend you look at the following table which in addition to the previously mentioned serializers includes the DataContractSerializer, NetDataContractSerializer and protobuf-net.

这篇关于什么是XmlSerializer的和BinaryFormatter的之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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