Dictionary序列化和反序列化
public class SerializeHelper
{
public static string XmlSerialize(List<CustomSearchEntity> obj)
{
XmlSerializer serializer = new XmlSerializer();
return serializer.Serialization(obj, typeof(List<CustomSearchEntity>));
} public static List<CustomSearchEntity> XmlDeserialize(string xmlStr, Dictionary<string, string> historySearchKeyMapping)
{
List<CustomSearchEntity> list = new List<CustomSearchEntity>();
XmlSerializer serializer = new XmlSerializer();
byte[] array = Encoding.UTF8.GetBytes(xmlStr);
MemoryStream stream = new MemoryStream(array);
list = (List<CustomSearchEntity>)serializer.Deserialize(stream, typeof(List<CustomSearchEntity>));
list.ForEach(item =>
{
if (historySearchKeyMapping.Keys.Contains(item.CustomSearchCategory))
{
item.CustomSearchCategory = historySearchKeyMapping.FirstOrDefault(p => p.Key == item.CustomSearchCategory).Value;
}
});
return list; }
} public class DictionarySerializeHelper
{
public static string SerializeDictionary(Dictionary<string, string> dataitems)
{
List<DataItem> tempdataitems = new List<DataItem>(dataitems.Count);
foreach (string key in dataitems.Keys)
{
tempdataitems.Add(new DataItem(key, dataitems[key].ToString()));
}
XmlSerializer xs = new XmlSerializer();
return xs.Serialization(tempdataitems, typeof(List<DataItem>));
} //Proj_10969;XAB-3422;Ken
public static Dictionary<string, string> DeserializeDictionary(string RawData, Dictionary<string, string> historySearchKeyMapping)
{
Dictionary<string, string> myDictionary = new Dictionary<string, string>();
XmlSerializer xs = new XmlSerializer();
byte[] array = Encoding.UTF8.GetBytes(RawData);
MemoryStream stream = new MemoryStream(array);
List<DataItem> templist = (List<DataItem>)xs.Deserialize(stream, typeof(List<DataItem>));
foreach (DataItem di in templist)
{
if (historySearchKeyMapping.Keys.Contains(di.Key))
{
myDictionary.Add(historySearchKeyMapping.FirstOrDefault(p => p.Key == di.Key).Value, di.Value);
}
else
{
myDictionary.Add(di.Key, di.Value);
}
}
return myDictionary;
}
} public class DataItem
{
public DataItem()
{
}
public string Key;
public string Value; public DataItem(string key, string value)
{
Key = key;
Value = value;
}
}
Dictionary序列化和反序列化的更多相关文章
- Dictionary 序列化与反序列化
[转:http://blog.csdn.net/woaixiaozhe/article/details/7873582] 1.说明:Dictionary对象本身不支持序列化和反序列化,需要定义一个继承 ...
- c# Json Dictionary序列化和反序列化
说明:Dictionary对象本身不支持序列化和反序列化,需要定义一个继承自Dictionary, IXmlSerializable类的自定义类来实现该功能.感觉完全可以把这样的类封装到C#库中,很具 ...
- XPatchLib 对象增量数据序列化及反序列化器 For .Net
在日常的软件开发和使用过程中,我们发现同一套系统的同一配置项在不同的客户环境中是存在各种各样的差异的.在差异较为分散时,如何较好的管理这些差异,使得维护过程能够更加安全和快速,一直在这样那样的困扰着开 ...
- c# Json 自定义类作为字典键时,序列化和反序列化的处理方法
一般情况下,Newtonsoft.Json.dll 对 Dictionary<int,object>.Dictionary<string,object>等序列化与反序列化都是成 ...
- Asp.net中Json的序列化和反序列化(二)
三.JSON序列化和反序列化日期时间的处理 JSON格式不直接支持日期和时间.DateTime值值显示为“/Date(700000+0500)/”形式的JSON字符串,其中第一个数字(在提供的示例中 ...
- c# XML序列化与反序列化
c# XML序列化与反序列化 原先一直用BinaryFormatter来序列化挺好,可是最近发现在WinCE下是没有办法进行BinaryFormatter操作,很不爽,只能改成了BinaryWrite ...
- .net学习之集合、foreach原理、Hashtable、Path类、File类、Directory类、文件流FileStream类、压缩流GZipStream、拷贝大文件、序列化和反序列化
1.集合(1)ArrayList内部存储数据的是一个object数组,创建这个类的对象的时候,这个对象里的数组的长度为0(2)调用Add方法加元素的时候,如果第一次增加元神,就会将数组的长度变为4往里 ...
- ASP.NET中JSON的序列化和反序列化
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介 ...
- ASP.NET 中JSON 的序列化和反序列化
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...
随机推荐
- 解决chi_sim.traineddata报read_params_file: parameter not found: allow_blob_division
在使用语音库时候 遇到报错:allow_blob_division,例如使用chi_sim.traineddata;在chi_sim.traineddata(注意版本)文件目录下,使用命令行执行: c ...
- 转载:android.屏幕锁,解锁,在取证上的应用
中国司法-鉴定,2013年第06期杂志
- 奇怪吸引子---YuWang
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- 【C++沉思录】句柄1
1.在[C++沉思录]代理类中,使用了代理类,存在问题: a.代理复制,每次创建一个副本,这个开销有可能很大 b.有些对象不能轻易创建副本,比如文件2.怎么解决这个问题? 使用引用计数句柄,对动态资源 ...
- 浏览器 CSS Hack 收集
所谓的Hack就是只有特定浏览器才能识别这段hack代码.Hack 不是什么好东西,除非没有办法,我们尽量还是不要用着玩意. 下面是各个浏览器的CSS Hack 列表. Firefox 浏览器 @-m ...
- mysql自动化安装
MySQL安装一般使用RPM或者源码安装的方式.RPM安装的优点是快速,方便.缺点是不能自定义安装目录.如果需要调整数据文件和日志文件的存放位置,还需要进行一些手动调整.源码安装的优点是可以自定义安装 ...
- 你可能不知道的python
1.如何循环获得下标,使用 enumerate ints = ['a','b','c','d','e','f'] for idx, val in enumerate(ints): print idx, ...
- int跟byte[]数组互转的方法,整数 + 浮点型
整数: int转byte数组 public static byte[] intToBytes2(int n){ ]; ;i < ;i++) { b[i]=(-i*)); } return b; ...
- WP-PostViews的安装和设置方法
wordpress本身并没有文章浏览统计功能,必须借助插件.想要知道自己的文章被多数访客浏览,或者访客对哪些文章或者哪类文章更加有兴趣,这就是文章统计的重要性了.WP-PostViews插件是哥不错的 ...
- Solved: Qt Library LNK 2001 staticMetaObject error
在链接Qt的库,比如QtGui4.lib,我这里是在链接QtSolutions_PropertyBrowser-head.lib的时候出现的链接错误.大概是说一个"XXXX::s ...