有关于Dictionary序列化,网上有许多。不过我都是没有成功应用;所以共享成功应用案例代码。

1.Dictionary本身是不支持序列化的,所以必须继承IXmlSerializable.

2.使用StringWriter进行最终String的转换工作,使用MemoryStream进行String输出,还是有异常报错。如:“十六进制Error!”

案例代码如下:

Step 1: Create Class ,Name Call SerializableDictionary

public class SerializableDictionary<TKey , TValue> 
           : Dictionary<TKey , TValue> , IXmlSerializable {
        #region IXmlSerializable 成员

public XmlSchema GetSchema ( ) {
            return null;
        }

/// <summary>
        /// 反序列化
         /// </summary>
        /// <param name="reader"></param>
        public void ReadXml ( XmlReader reader ) {
            XmlSerializer keySerializer = new XmlSerializer( typeof( TKey ) );
            XmlSerializer valueSerializer = new XmlSerializer( typeof( TValue ) );
            if ( reader.IsEmptyElement || !reader.Read() ) {
                return;
            }
            while ( reader.NodeType != XmlNodeType.EndElement ) {
                reader.ReadStartElement( "item" );

reader.ReadStartElement( "key" );
                TKey key = ( TKey )keySerializer.Deserialize( reader );
                reader.ReadEndElement();
                reader.ReadStartElement( "value" );
                TValue value = ( TValue )valueSerializer.Deserialize( reader );
                reader.ReadEndElement();

reader.ReadEndElement();
                reader.MoveToContent();
                this.Add( key , value );
            }
            reader.ReadEndElement();
        }

/// <summary>
        /// 序列化
        /// </summary>
        /// <param name="writer"></param>
        public void WriteXml ( XmlWriter writer ) {
            XmlSerializer keySerializer = new XmlSerializer( typeof( TKey ) );
            XmlSerializer valueSerializer = new XmlSerializer( typeof( TValue ) );

foreach ( TKey key in this.Keys ) {
                writer.WriteStartElement( "item" );

writer.WriteStartElement( "key" );
                keySerializer.Serialize( writer , key );
                writer.WriteEndElement();
                writer.WriteStartElement( "value" );
                valueSerializer.Serialize( writer , this[key] );
                writer.WriteEndElement();

writer.WriteEndElement();
            }
        }

#endregion
    }

Step 2: Create Entity,Call Enity

    [Serializable]
    public class Entity {
        private Int32 id;

public Int32 ID {
            get { return id; }
            set { id = value; }
        }
    }

Step 3:Ready Demo Data

public static SerializableDictionary<Int32 , List<Entity>> GetDict ( ) {
     SerializableDictionary<Int32 , List<Entity>> dict =

new SerializableDictionary<int , List<Entity>>();
            {
                List<Entity> el = new List<Entity>();
                {
                    Entity entity = new Entity();
                    {
                        entity.ID = 1;
                    }
                    el.Add( entity );
                }
                dict.Add( 1 , el );
            }
            return dict;
        }

Step 4:Coding Serializer and Deserialize For Dictionary

  public static String DictionarySerializer ( Object obj , System.Type type ) {
            using ( StringWriter sw = new StringWriter() ) {
                XmlSerializer ser = new XmlSerializer( type );
                ser.Serialize( sw , obj );
                return sw.ToString();
            }
        }

public static Object DictionaryDeserialize ( Type type , String str ) {
            using ( StringReader sr = new StringReader( str ) ) {
                XmlSerializer xz = new XmlSerializer( type );
                return xz.Deserialize( sr );
            }
        }

Step 5:WebService and AnyWhere ,Apply to this Case

Service Serialize:(服务方序列化)
[WebMethod]
public String GetX ( Parameter query ) {
      return DictionarySerializer( Object , 
             typeof( SerializableDictionary<Int32 , List<Entity>> ) );
}

Client Deserialize:(客户方反序列化)

SerializableDictionary<Int32 , List<Entity>> sd = 
     DictionaryDeserialize( typeof( SerializableDictionary<Int32 , List<Entity>> ) 
                           ,GetX( query ) )
     as SerializableDictionary<Int32 , List<Entity>>;

Step 6: Output Results

<?xml version="1.0" encoding="utf-16"?>
<SerializableDictionaryOfInt32ListOfEntity>
  <item>
    <key>
      <int>1</int>
    </key>
    <value>
      <ArrayOfEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <Entity>
          <ID>1</ID>
        </Entity>
      </ArrayOfEntity>
    </value>
  </item>
</SerializableDictionaryOfInt32ListOfEntity>

参考资源:

http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry

Dictionary Serializer(Dictionary 序列化)相关推荐

  1. drf serializer 的序列化

    drf serializer 的序列化 文章目录 drf serializer 的序列化 1.序列化类 常用字段类 及字段参数 1.1.常用字段类 1.2.常用字段参数 2. 序列化类高级用法之sou ...

  2. 接口,常用接口,列表ArrayList,泛型,Dictionary(字典),Dictionary版-火星文翻译器,其他集合类...

    接口 接口相当于没有方法实现的抽象类. public interface Flyable { void Flay(); } 接口方法不要public.(为什么不能用private,public,因为如 ...

  3. python corpora.Dictionary corpus dictionary.doc2bow 词袋模型转为稀疏矩阵 词向量 不要词袋模型

    from gensim import matutils a = matutils.corpus2csc(corpus).toarray().T a

  4. djangorestframework源码分析2:serializer序列化数据的执行流程

    djangorestframework源码分析 本文环境python3.5.2,djangorestframework (3.5.1)系列 djangorestframework源码分析-serial ...

  5. 自然语言处理--文档集数据处理 gensim corpora.Dictionary

    corpora基本概念: corpora是gensim中的一个基本概念,是文档集的表现形式,也是后续进一步处理的基础.从本质上来说,corpora其实是一种格式或者说约定,其实就是一个二维矩阵.在实际 ...

  6. 技术图文:浅析 C# Dictionary实现原理

    背景 对于 C# 中的 Dictionary类 相信大家都不陌生,这是一个 Collection(集合) 类型,可以通过 Key/Value (键值对) 的形式来存放数据:该类最大的优点就是它查找元素 ...

  7. C#中的Dictionary简介

    C#中的Dictionary简介  http://www.cnblogs.com/ccczqh/archive/2011/01/04/1925852.html 简介 在C#中,Dictionary提供 ...

  8. AS3中Object与Dictionary的区别

    AS3中的Dictionary类(flash.utils.Dictionary)是一个新的AS类. Dictionary类和Object唯一的区别在于: Dictionary对象可以使用非字符串作为键 ...

  9. Swift字典Dictionary快速文档

    Swift Dictionary字典 简述 字典就是通过Key - Value对应关系的数据结构. swift与OC字典区别 ​ 类型: Swift字典是值类型, OC字典类型为引用类型 初始化方法 ...

最新文章

  1. HP 3055 恢复出厂
  2. java 参数 string_java(String和StringBuffer分别作为参数传递)
  3. 使用R完成决策树分类
  4. 为某个酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能。 1、该系统的用户是:酒店前台。 2、酒店使用一个二维数组来模拟。“Room[][] roo
  5. Spark TopN
  6. html5的video怎么把里面的控制器移出来_电动车突然不走了,但控制器和转把没坏,但车走电,这是怎么回事...
  7. java浮点运算很难_关于Java:浮点运算不能产生精确结果
  8. ArcGIS for Android 100.3.0(1):开发环境配置
  9. QT实现操控打印机打印图片
  10. 软考论文案例-论微服务架构及其应用
  11. 核磁共振电子计算机断层扫描术,计算机断层扫描(CT)和核磁共振(MRI) 的区别...
  12. 【Kind2(基于SMT的自动模型检查器)学习笔记】基本语法
  13. 多媒体计算机用什么音箱好,5款性价比高的电脑小音箱推荐(每一款音质都相当ok)...
  14. 我裸辞 转行软件测试 然而没有人要我
  15. c++求一个数的因子
  16. 华为路由交换学习篇-路由
  17. Message中obtain()与recycle()
  18. 思维模型 STAR原则
  19. 每天一个效果 :(14)抽奖器功能
  20. STM32芯片VDD、VDDA和VREF的关系

热门文章

  1. Juniper SRX 常用命令
  2. python--windows下安装BeautifulSoup
  3. 软考中项学习之路--在路上
  4. 2015年下半年计划
  5. Java中的异常和递归
  6. 世博、城市云和2020
  7. PowerPath/VE:EMC夯实虚拟环境的独门武器
  8. 学学这个垃圾×××网站怎么埋头赚大钱的!
  9. BTC缺乏关键性突破仍需调整,BCH强势姿态保持引领全场
  10. web请求报出 “超过了最大请求长度” 【注意:重启IIS】