.net - 使用 .Net 使用 XmlSerializer 有什么限制(如果有的话)?

标签 .net xml serialization

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

最佳答案

我通常发现 XmlSerializer 对于任何不仅仅是 DTO 的 POCO 来说都是一个糟糕的选择。如果您需要特定的 XML,您可以选择 Xml*Attribute 和/或 IXmlSerializable 路线 - 但您会得到一个非常困惑的对象。

出于某些目的,它仍然是一个显而易见的选择 - 即使它有局限性。但是,对于简单的存储和重新加载数据,我发现 BinaryFormatter 是一个更容易的选择,而且陷阱更少。

这是 XmlSerializer 的一些烦恼的列表 - 大多数我曾在某个时候被咬过,其他的我在 MSDN 找到了:

  • 需要一个公共(public)的、无参数的构造函数
  • 仅序列化公共(public)读/写属性和字段
  • 要求知道所有类型
  • 实际调用 get_* 和 set_*,因此将运行验证等。这可能是好事也可能是坏事(还要考虑调用的顺序)
  • 只会序列化符合特定规则的 IEnumerable 或 ICollection 集合

The XmlSerializer gives special treatment to classes that implement IEnumerable or ICollection. A class that implements IEnumerable must implement a public Add method that takes a single parameter. The Add method's parameter must be of the same type as is returned from the Current property on the value returned from GetEnumerator, or one of that type's bases.

A class that implements ICollection (such as CollectionBase) in addition to IEnumerable must have a public Item indexed property (indexer in C#) that takes an integer, and it must have a public Count property of type integer. The parameter to the Add method must be the same type as is returned from the Item property, or one of that type's bases. For classes that implement ICollection, values to be serialized are retrieved from the indexed Item property, not by calling GetEnumerator.

  • 不序列化 IDictionary
  • 使用动态生成的程序集,这些程序集可能不会从应用程序域中卸载。

To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified types. The infrastructure finds and reuses those assemblies. This behavior occurs only when using the following constructors:

XmlSerializer.XmlSerializer(Type) XmlSerializer.XmlSerializer(Type, String)

If you use any of the other constructors, multiple versions of the same assembly are generated and never unloaded, which results in a memory leak and poor performance.

  • 无法序列化 ArrayList[] 或 List[]
  • 还有其他奇怪的边缘情况

The XmlSerializer cannot be instantiated to serialize an enumeration if the following conditions are true: The enumeration is of type unsigned long (ulong in C#) and the enumeration contains any member with a value larger than 9,223,372,036,854,775,807.

The XmlSerializer class no longer serializes objects that are marked as [Obsolete].

You must have permission to write to the temporary directory (as defined by the TEMP environment variable) to deserialize an object.

  • 需要阅读 .InnerException 以获得有关错误的任何有用信息

关于.net - 使用 .Net 使用 XmlSerializer 有什么限制(如果有的话)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/109318/

相关文章:

c# - 为什么 System.TimeSpan 不是可序列化的 XML?

Ubuntu 和 Win10 上的 C#.Net MySQL 数据库连接

c# - 检查 XElement 是否全局为 null

c# - RabbitMQ 未定义 : There is no template at js/tmpl/login. ejs

XML 解析错误 : junk after document element

c# - 复制 xml 文件并在特定位置插入新元素 - C#

java - 可序列化和外部化之间的性能差异 (Java)

c# - 如何将c#中的xml数据反序列化为java?

.net - 从 Coldfusion 运行 .NET dll

.net - 特定年份和月份的星期六和星期日的数量