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. 声色贴生成图片总结 Imagick

    2014-08-24 都是按以前的程序进行了,但去年8月都可以用Imagick正常生成CMYK的图片,但今天就是不行. 经过一切测试方法及思路,解决方法如下. 问题主要出现在: 生成的二维码是RGB格 ...

  2. Hadoop - Mac OSX下配置和启动hadoop以及常见错误解决

    0. 安装JDK 参考网上教程在OSX下安装jdk 1. 下载及安装hadoop a) 下载地址: http://hadoop.apache.org b) 配置ssh环境 在terminal里面输入: ...

  3. oracle 判断中文函数

    create or replace function func_chinese(  p_str     in varchar2,     -- 输入的字符串  p_code    in varchar ...

  4. 最近读的javascript,一些文章

    本帖子是记录一些javascript的一些文章: 1. 理解node.js 2.异步编程 http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF% ...

  5. Python - 升级所有已安装的第三方包

    我们有时候需要把系统上已经安装的第三方的packages升级到最新版.但是easy_install和pip都没有直接的命令可以使用. 我们可以是用如下命令来查看系统上面哪些包过期了. pip list ...

  6. oracle中查询某张表都被哪些表参照了

    起因: 系统测试的时候发现如果某条记录已经被引用了,这个时候删除这条记录会引起数据不一致,系统会报错.比如警员信息,在考勤记录表里会引用警员ID,如果考勤记录表中已经存在这个警员ID了,这时从警员表中 ...

  7. 数据库中字段类型对应的C#中的数据类型

    数据库中字段类型对应C#中的数据类型: 数据库                 C#程序 int int32 text string bigint int64 binary System.Byte[] ...

  8. dapper 操作类封装

    using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using S ...

  9. FMDB 版本迁移

    FMDB 版本迁移 安装 1. 通过CocoaPods 在项目根目录创建并编辑 Podfile 内容如下 $ cd /path/to/MyProject $ touch Podfile $ edit ...

  10. SQL语句调优 - 索引上的数据检索方法

    如果一张表上没有聚集索引,数据将会随机的顺序存放在表里.以dbo.SalesOrderDetail_TEST为例子.它的上面没有聚集索引,只有一个在SalesOrderID上的非聚集索引.所以表格的每 ...