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. “String.h” 源代码总结

    <String.h>  总结: 常用的函数:   一.memchr: 说明:当第一次遇到字符ch时停止查找.如果成功,返回指向字符ch的指针:否则返回NULL. 代码: #include ...

  2. 实现WebService的调用与被调用

    之前一直用WCF来开发服务,可是从未用过WebService.对WebService有种很神奇的期待,都说WebService比较简单,但是从未用过就对我来说就是一种新的知识.起始让我来说WCF与We ...

  3. 昨天面试新浪 java试题

      昨天去了新浪网面试,感觉新浪真的挺不错的,工作环境那叫一个好啊.对于一般屌丝的话进到这种公司就可以呆一辈子了.做了面试之后感觉不管大公司还是小公司都还是注重基础和你平时工作的积累的.所以不能抱怨现 ...

  4. [Usaco2008 Feb]Line连线游戏[暴力][水题]

    Description Farmer John最近发明了一个游戏,来考验自命不凡的贝茜.游戏开始的时 候,FJ会给贝茜一块画着N (2 <= N <= 200)个不重合的点的木板,其中第i ...

  5. GLIBC_2.7升级

    GLIBC_2.7: ftp://ftp.ntua.gr/pub/FreeBSD/ports/distfiles/rpm/i386/fedora/8/glibc-2.7-2.i386.rpm ftp: ...

  6. ASP.NET MVC相关

    Orchard源码分析(7):ASP.NET MVC相关 概述 Orchard归根结底是一个ASP.NET MVC(以后都简称为MVC)应用,但在前面的分析中,与MVC相关内容的涉及得很少.MVC提供 ...

  7. url在线编码和解码

    在工作中,经常遇到encode之后的url.想查看里面的某个参数的时候,很不直观.今天在网上搜了一下对url在线编码和解码的网站.对我来说,使用起来很方便.而且这个网站里面,不仅仅有对url的编码和解 ...

  8. poj2236无线网络

    这一题的大意:在救灾当中需要用网络,这堆人就用笔记本建了一个无线网,但是来,互相通信都是有距离限制的,一台电脑只能和距离他为d的电脑通信,然后一台电脑也可以通过几台电脑搭成线这样通信.然后就是输入每台 ...

  9. 《12个有趣的C语言问答》评析2

    <12个有趣的C语言问答>评析(2) 前文链接:http://www.cnblogs.com/pmer/p/3313913.html (没存盘,遭遇过热保护.至少4个问答的评论白写了.默哀 ...

  10. MongoDB学习3

    MongoDB学习(翻译3)   支持的where字句(比较多,今天先写一部分) 本节介绍支持的where字句 正如前面提到的,不是所有的C#表达式都支持where子句.您可以以此文为指导,或者你可以 ...