[转:http://blog.csdn.net/woaixiaozhe/article/details/7873582]

1.说明:Dictionary对象本身不支持序列化和反序列化,需要定义一个继承自Dictionary, IXmlSerializable类的自定义类来实现该功能。感觉完全可以把这样的类封装到C#库中,很具有通用性嘛,至今没有遇到不能用的情况的说,或许出于其他方面的考虑microsoft才没有这么做。

2.SerializableDictionary自定义类

     [Serializable]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
public SerializableDictionary() { }
public void WriteXml(XmlWriter write) // Serializer
{
XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue)); foreach (KeyValuePair<TKey, TValue> kv in this)
{
write.WriteStartElement("SerializableDictionary");
write.WriteStartElement("key");
KeySerializer.Serialize(write, kv.Key);
write.WriteEndElement();
write.WriteStartElement("value");
ValueSerializer.Serialize(write, kv.Value);
write.WriteEndElement();
write.WriteEndElement();
}
}
public void ReadXml(XmlReader reader) // Deserializer
{
reader.Read();
XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue)); while (reader.NodeType != XmlNodeType.EndElement)
{
reader.ReadStartElement("SerializableDictionary");
reader.ReadStartElement("key");
TKey tk = (TKey)KeySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement("value");
TValue vl = (TValue)ValueSerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadEndElement();
this.Add(tk, vl);
reader.MoveToContent();
}
reader.ReadEndElement(); }
public XmlSchema GetSchema()
{
return null;
}
}

3.使用

a.定义SerializableDictionary对象,这里以存储<string,string>键对为例:

 SerializableDictionary<string, string> serializableDictionary = new SerializableDictionary<string, string>();  

b.添加元素

 serializableDictionary.Add("Key1", “Value1”);  

c.序列化

 using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
{
XmlSerializer xmlFormatter = new XmlSerializer(typeof(SerializableDictionary<string, string>));
xmlFormatter.Serialize(fileStream, this.serializableDictionary);
}

注:文件名fileName自己定义,如“file.xml”

d.反序列化

 using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
{
XmlSerializer xmlFormatter = new XmlSerializer(typeof(SerializableDictionary<string, string>));
this.serializableDictionary = (SerializableDictionary<string,string>)xmlFormatter.Deserialize(fileStream);
}

Dictionary 序列化与反序列化的更多相关文章

  1. c# Json Dictionary序列化和反序列化

    说明:Dictionary对象本身不支持序列化和反序列化,需要定义一个继承自Dictionary, IXmlSerializable类的自定义类来实现该功能.感觉完全可以把这样的类封装到C#库中,很具 ...

  2. Dictionary序列化和反序列化

    public class SerializeHelper { public static string XmlSerialize(List<CustomSearchEntity> obj) ...

  3. XPatchLib 对象增量数据序列化及反序列化器 For .Net

    在日常的软件开发和使用过程中,我们发现同一套系统的同一配置项在不同的客户环境中是存在各种各样的差异的.在差异较为分散时,如何较好的管理这些差异,使得维护过程能够更加安全和快速,一直在这样那样的困扰着开 ...

  4. c# Json 自定义类作为字典键时,序列化和反序列化的处理方法

    一般情况下,Newtonsoft.Json.dll 对 Dictionary<int,object>.Dictionary<string,object>等序列化与反序列化都是成 ...

  5. Asp.net中Json的序列化和反序列化(二)

     三.JSON序列化和反序列化日期时间的处理 JSON格式不直接支持日期和时间.DateTime值值显示为“/Date(700000+0500)/”形式的JSON字符串,其中第一个数字(在提供的示例中 ...

  6. c# XML序列化与反序列化

    c# XML序列化与反序列化 原先一直用BinaryFormatter来序列化挺好,可是最近发现在WinCE下是没有办法进行BinaryFormatter操作,很不爽,只能改成了BinaryWrite ...

  7. .net学习之集合、foreach原理、Hashtable、Path类、File类、Directory类、文件流FileStream类、压缩流GZipStream、拷贝大文件、序列化和反序列化

    1.集合(1)ArrayList内部存储数据的是一个object数组,创建这个类的对象的时候,这个对象里的数组的长度为0(2)调用Add方法加元素的时候,如果第一次增加元神,就会将数组的长度变为4往里 ...

  8. ASP.NET中JSON的序列化和反序列化

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介 ...

  9. ASP.NET 中JSON 的序列化和反序列化

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...

随机推荐

  1. Android doc打开太慢

    C:\Windows\System32\drivers\etc\HOSTS 127.0.0.1 fonts.googleapis.com 127.0.0.1 www.google.com 127.0. ...

  2. row_number() OVER(PARTITION BY)函数介绍

      OVER(PARTITION BY)函数介绍 开窗函数               Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个 ...

  3. angular directive指令内的参数

    angular.module('myApp', []) .directive('myDirective', function() { return { restrict: String, priori ...

  4. xcodebuild

    xcodebuild -workspace /path/union/moon-ios/Moon.xcworkspace -scheme Moon ONLY_ACTIVE_ARCH=NO TARGETE ...

  5. docker初学笔记

    什么是docker 不准确的说,docker是一种轻量级的虚拟机,它把可执行文件和运行环境打包成一个image,放在容器里运行,但是启动速度比虚拟机快很多,资源消耗小.这种技术主要是为了解决部署环境的 ...

  6. VaildForm 自定义提示消息

    ValidForm插件提供了7种提示效果,其中有四种自定义效果,具体访问地址:http://validform.rjboy.cn/demo.html 个人偏爱其中两种,即 l 提示效果四:[自定义提示 ...

  7. Zabbix汉化方法

    1.windows下选择一个汉化字体包 2.拷贝到linux字体目录下 [root@www Desktop]# cd /var/www/html/zabbix/fonts/[root@www font ...

  8. Galaxy Classification

    10.3 Data Preparation After removing a large number of the columns from the raw SDSS dataset, introd ...

  9. Java中正则表达式及其常用类Math、Calendar、Date、BigDecimal、BigInterger、System、Rondom的使用

    1:正则表达式(理解) (1)就是符合一定规则的字符串 (2)常见规则 A:字符 x 字符 x.举例:'a'表示字符a \\ 反斜线字符. \n 新行(换行)符 ('\u000A') \r 回车符 ( ...

  10. 大公司的Java面试题集

    找工作要面试,有面试就有对付面试的办法.以下一些题目来自我和我朋友痛苦的面试经历,提这些问题的公司包括IBM, E*Trade, Siebel, Motorola, SUN, 以及其它大小公司. 面试 ...