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序列化和反序列化的更多相关文章

  1. Dictionary 序列化与反序列化

    [转:http://blog.csdn.net/woaixiaozhe/article/details/7873582] 1.说明:Dictionary对象本身不支持序列化和反序列化,需要定义一个继承 ...

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

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

  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. 【转】iOS 10 UserNotifications 使用说明

    注意:XCode8的需要手动开启主target Capabilities中的Push Notification. 关于创建多个target后真机测试的证书问题,除了主target手动创建开发和发布证书 ...

  2. GitHub 优秀的 Android 开源项目(转)

    今天查找资源时看到的一篇文章,总结了很多实用资源,十分感谢原作者分享. 转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介 ...

  3. leveldb - 并发写入处理

    在并发写入的时候,leveldb巧妙地利用一个时间窗口做batch写入,这部分代码值得一读: Status DBImpl::Write(const WriteOptions& options, ...

  4. 深入剖析 redis RDB 持久化策略

    简介 redis 持久化 RDB.AOF redis 提供两种持久化方式:RDB 和 AOF.redis 允许两者结合,也允许两者同时关闭. RDB 可以定时备份内存中的数据集.服务器启动的时候,可以 ...

  5. 在Windows 2008/2008 R2 上配置IIS 7.0/7.5 故障转移集群

    本文主要是从:http://support.microsoft.com/kb/970759/zh-cn,直接转载,稍作修改裁剪而来,其中红色粗体部分,是我特别要说明的 若要配置 IIS 7.0 和 7 ...

  6. 4kbps~15kbps语音编码器基础框架ACELP

  7. Notes on how to use Webots, especially how to make a robot fly in the air

    How to create a new project Wizard - New project directory   Scene Tree Scene tree is a representati ...

  8. c++中的&

    变量的前面表示取变量地址赋值给指针, 如:int a = 0; int *pa = &a;类型后面表示引用,引用即变量的替身. int a = 0; int &ref = a;操作re ...

  9. Android实现自适应正方形GridView(陌陌引导页面效果)

    1.http://blog.chengyunfeng.com/?p=465 2.备注,慢慢研究

  10. 常用SQL语句备忘录

    1.---表中有重复记录用SQL语句查询出来 select * from Recharge where RechargeSerial in (select RechargeSerial from Re ...