WCF 序列化 - 更多信息
我读了一些微软的文章。他们解释说WCF使用DataContractSerializer
进行序列化。但是这些文章没有解释为什么DataContractSerializer优先于 XmlSerialization。任何人都可以给我附加信息吗?
I read some microsoft articles.They explained that WCF uses DataContractSerializer
for serialization.But the articles did not explain why DataContractSerializer preferred over
XmlSerialization.Can anyone give me the additional information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此处是一篇带有比较的文章。
关键部分:
XmlSerializer
优点:
缺点:
只能序列化属性
属性必须是公共的
属性必须有一个 get 和一个 set,这可能会导致一些尴尬的设计
支持较窄的类型集
无法理解 DataContractAttribute,除非也有 SerializedAttribute,否则不会序列化它
DataContractSerializer
优点:
选择加入而不是选择退出属性进行序列化。这意味着您指定要序列化的内容
因为它是选择的,所以您不仅可以序列化属性,还可以序列化字段。您甚至可以序列化非公共成员,例如私有或受保护的成员。而且您也不需要对属性进行设置(但是如果没有设置器,您可以序列化,但不能反序列化)
序列化数据比 XmlSerializer 快大约 10%,因为您无法完全控制数据的方式序列化,可以做很多事情来优化序列化/反序列化过程。
能够理解SerializedAttribute并知道需要序列化
更多选项和对 KnownTypes 的控制
缺点:
Here is an article with a comparison.
Key section:
XmlSerializer
Advantages:
Disadvantages:
Can only serialize properties
Properties must be public
Properties must have a get and a set which can result in some awkward design
Supports a narrower set of types
Cannot understand the DataContractAttribute and will not serialize it unless there is a SerializableAttribute too
DataContractSerializer
Advantages:
Opt-in rather than opt-out properties to serialize. This mean you specify what you want serialize
Because it is opt in you can serialize not only properties, but also fields. You can even serialize non-public members such as private or protected members. And you dont need a set on a property either (however without a setter you can serialize, but not deserialize)
Is about 10% faster than XmlSerializer to serialize the data because since you don’t have full control over how it is serialize, there is a lot that can be done to optimize the serialization/deserialization process.
Can understand the SerializableAttribute and know that it needs to be serialized
More options and control over KnownTypes
Disadvantages: