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的简单介绍 ...
随机推荐
- 安装配置NFS服务
超级好的配置centos下服务的链接 http://www.server-world.info/en/note?os=CentOS_6&p=nfs http://linux.vbird.org ...
- COM是一个更好的C++
昨天看了<COM本质论>的第一章”COM是一个更好的C++”,觉得很有必要做一些笔记,于是整理成这篇文章,我相信你值得拥有. 这篇文章主要讲的内容是:一个实现了快速查找功能的类FastSt ...
- python排序算法的实现-选择
1.算法: 对于一组关键字{K1,K2,…,Kn}, 首先从K1,K2,…,Kn中选择最小值,假如它是 Kz,则将Kz与 K1对换: 然后从K2,K3,… ,Kn中选择最小值 Kz,再将Kz与K2对换 ...
- java Clone之深浅拷贝
要点: 1.浅度拷贝可以不实现Cloneable接口(自动使用Object.clone)或者不重写Cloneable的clone方法. 2.要被深度拷贝的类必须实现Cloneable接口并重写clon ...
- 3.C#中泛型类的进一步探讨
阅读目录 一:多重泛型 class不仅可以有T,还可以有K,实例化的时候传多个数据类型的类型,C#集合类型中的Dictionary就是多重泛型 using System; using System. ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- [leetcode]Combine Two Tables
leetcode竟然有sql的题了..两道简单的应该会做 这个题主要就是一个left join... # Write your MySQL query statement below SELECT P ...
- [leetode]Binary Search Tree Iterator
用个stack模拟递归即可 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * Tr ...
- [转]c++流缓冲---rdbuf()
C++标准库封装了一个缓冲区类streambuf,以供输入输出流对象使用.每个标准C++输出输出流对象都包含一个指向streambuf的指针,用 户可以通过调用rdbuf()成员函数获得该指针,从而直 ...
- android EditText inputType说明
在开发的过程中,通常会用到EditText,如何让虚拟键盘来适应输入框中内容的类型,通常我们都会在xml文件中加入android:inputType="". android:inp ...