Having just recently ran into some major serialization issues I’m going to list some of the errors and pitfalls that I ran into.

Some of the errors encountered

Error one
“<ClassName> is inaccessible due to its protection level. Only public types can be processed.”

It means that the Class you are trying to serialize is not marked as public and hence the serializor can not access it. Depending on the Class scope this may not be a problem e.g. the serialization code and the class are both in the same scope.

Error Two
“Cannot serialize member <Property Name> of type <Type> because it is an interface.”

It means that one of the members in your class is defined as an interface. An interface can never be serialized since the serializor will not know which instance of the interface to use.

Error Three
The type <Type> was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

This error is caused when trying to serialize an inherited class

E.g. the following example will cause this problem

  1. public class Test
  2. {
  3. private string _Field = "";
  4. public string Field
  5. {
  6. get { return _Field; }
  7. set { _Field = value; }
  8. }
  9. }
  10. public class TestInherited : Test
  11. {
  12. }
  13. public class Container
  14. {
  15. private Test ivField;
  16. public Test Field
  17. {
  18. get { return _Field; }
  19. set { _Field = value; }
  20. }
  21. }
  22. Container _Test = new Container();
  23. _Test.Field = new TestInherited();

The issue is that the Container.Field is defined as Test but instantiated as TestInherited. Their are two solutions for this problem
1) Add the attribute [XmlInclude(typeof(TestInherited))] to the Test class
2) new XmlSerializer(typeof(Container), new Type[] { typeof(TestInherited) });

The second method is preferred. With the first method you have to decorate your base classes to which you may not always have access to, secondly the base class should not be aware of any class that inherits from it.

Note:特别的,当一个对象中的属性为基类或者object类型时,他的依赖注入的类型(即运行时类型)必须明确使用 XmlInclude属性标明序列化的类型,否则序列化会出错。

转载自 http://www.johnsoer.com/blog/?p=125

Reprint: Serialization的更多相关文章

  1. Dubbo源码分析:Serialization

    背景 顺序化逻缉处理! 类图 获取Serialization对象时序图 序列化

  2. [.net 面向对象程序设计进阶] (12) 序列化(Serialization)(四) 快速掌握JSON的序列化和反序列化

    [.net 面向对象程序设计进阶] (12) 序列化(Serialization)(四) 快速掌握JSON的序列化和反序列化 本节导读: 介绍JSON的结构,在JS中的使用.重点说明JSON如何在.N ...

  3. C++的性能C#的产能?! - .Net Native 系列《二》:.NET Native开发流程详解

    之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...

  4. boost--序列化库serialization

    序列化可以把对象转化成一个字节流存储或者传输,在需要时再回复成与原始状态一致的等价对象.C++标准没有定义这个功能.boost.serialization以库的形式提供了这个功能,非常强大,可以序列化 ...

  5. Serialization之SOAP序列化

    前言 XML序列化还可用于对象序列化符合SOAP规范的XML流.SOAP是一种简单的基于XML的协议,它使应用程序通过HTTP来交换信息.专门为使用XML来传输过程调用而设计的.如同常规的XML序列化 ...

  6. 跟我一起学WCF(7)——WCF数据契约与序列化详解

    一.引言 在前面博文介绍到,WCF的契约包括操作契约.数据契约.消息契约和错误契约,前面一篇博文已经结束了操作契约的介绍,接下来自然就是介绍数据契约了.所以本文要分享的内容就是数据契约. 二.数据契约 ...

  7. LeetCode——Serialize and Deserialize Binary Tree

    Description: Serialization is the process of converting a data structure or object into a sequence o ...

  8. 297. Serialize and Deserialize Binary Tree

    题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...

  9. Java 远程通讯技术及原理分析

    在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如:RMI.MINA.ESB.Burlap.Hessian.SOAP.EJB和JMS等,这些 ...

随机推荐

  1. Matlab中如何用命令方式保存图像?

    命令很简单,例如下面这个代码将当前图像保存到F1.emf文件中,保存格式为emf saveas(gcf,'F.emf','emf'); 当然了,也可以保存为jpg格式,修改为: saveas(gcf, ...

  2. [翻译]HBase 的 MVCC 和内建的原子操作

    翻译一篇:HBase MVCC and built-in Atomic Operations 作者:Lars Hofhansl HBase 有一些特殊的原子操作: checkAndPut, check ...

  3. Remote验证及其改进(附源码)

    Remote验证及其改进(附源码) 表单中的输入项,有些是固定的,不变的验证规则,比如字符长度,必填等.但有些是动态的,比如注册用户名是否存在这样的检查,这个需要访问服务器后台才能解决.这篇文章将会介 ...

  4. 基于多重信号分类算法的DOA估计

    原创博文,转载请注明出处 下面的论文是我的雷达处理的作业,拿来共享,不喜勿喷.由于公式编辑器的原因,无法复制公式,全部内容请点击. 基于多重信号分类算法的DOA估计 1引言 多重信号分类(MUSIC) ...

  5. MongoDB应用介绍之前

    MongoDb企业应用实战(一) 写在MongoDB应用介绍之前   故事背景: 本人有幸,经老友( 现为x知名快递公司技术总监 ) 推荐进入中国前三大民营快递公司之一工作,在此非常感谢他,在此也非常 ...

  6. SublimeText编辑器替代notepad++了

    可以考虑使用SublimeText编辑器替代notepad++了   内容目录: 插件安装配置 配置打包下载 大概是去年吧,这款编辑器神一般的出现在我面前,经过我小心翼翼的试用后发现并不是那么太顺手, ...

  7. [RM 状态机详解2] RMAppAttempt状态机详解

    摘要 本文详细描述RMAppAttempt状态机内的状态与其转换关系,分析的代码基于Apache社区Hadoop最新的2.3.0版本. RMAppAttempt状态机 在RM中,一个RMApp可能对于 ...

  8. nc 简单的使用

    非常强大的网络工具nc netcat 下面自己总结了它的几种常用用法(参考了它的man): 1.聊天 ClientA: nc - ClientB: nc A'sIP 1234 2.数据传输 Clien ...

  9. DevExpress 学习使用之 TreeList

    1. 必须先添加列,否则不能显示任何节点内容 2. 如果是代码添加列时,一定要写明 VisibleIndex = 几,没有这句,不显示 3. 顶级结点用 TreeList.AppendNode 来添加 ...

  10. node.js系列笔记之node.js初识《一》

    node.js系列笔记之node.js初识<一> 一:环境说明 1.1 Linux系统CentOS 5.8 1.2 nodejs v0.10.15 1.3 nodejs源码下载地址 htt ...