使用 .Net 使用 XmlSerializer 有什么限制(如果有)? [英] Using .Net what limitations (if any) are there in using the XmlSerializer?

查看:17
本文介绍了使用 .Net 使用 XmlSerializer 有什么限制(如果有)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 .Net 使用 XmlSerializer 有什么限制(如果有)?例如,您可以将图像序列化为 XML 吗?

Using .Net what limitations (if any) are there in using the XmlSerializer? For example, can you serialize Images to XML?

推荐答案

XmlSerializer 有一些缺点.

The XmlSerializer has a few drawbacks.

  1. 它必须知道所有被序列化的类型.您不能通过代表序列化程序不知道的类型的接口传递一些东西.
  2. 它不能进行循环引用.
  3. 如果在对象图中多次引用,它将多次序列化同一个对象.
  4. 无法处理私有字段序列化.

我(愚蠢地)编写了自己的序列化程序来解决其中的一些问题.不要那样做;这是一项大量工作,几个月后您会发现其中存在细微的错误.我在编写自己的序列化程序和格式化程序时获得的唯一收获是对对象图序列化所涉及的细节有了更大的了解.

I (stupidly) wrote my own serializer to get around some of these problems. Don't do that; it is a lot of work and you will find subtle bugs in it months down the road. The only thing I gained in writing my own serializer and formatter was a greater appreciation of the minutia involved in object graph serialization.

我在 WCF 时发现了 NetDataContractSerializer出来.它可以完成 XmlSerializer 不具备的所有功能.它以类似于 XmlSerializer 的方式驱动序列化.一种是用属性装饰各种属性或字段,以通知序列化程序要序列化的内容.我用 NetDataContractSerializer 替换了我编写的自定义序列化程序,并且对结果非常满意.我强烈推荐它.

I found the NetDataContractSerializer when WCF came out. It does all the stuff from above that XmlSerializer doesn't do. It drives the serialization in a similar fashion to the XmlSerializer. One decorates various properties or fields with attributes to inform the serializer what to serialize. I replaced the custom serializer I had written with the NetDataContractSerializer and was very happy with the results. I would highly recommend it.

这篇关于使用 .Net 使用 XmlSerializer 有什么限制(如果有)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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