zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    

      

  • 相关阅读:
    洛谷:P3391 【模板】文艺平衡树(Splay)
    洛谷:P2234 [HNOI2002]营业额统计
    洛谷冬令营随想
    存储过程分页说明
    前台数据验证(1)
    前台数据验证(2)
    【转载】JQuery中$.ajax()方法参数详解
    启用IIS的Gzip压缩
    使用WCF常见问题
    在使用WCF时如何生成证书认证?
  • 原文地址:https://www.cnblogs.com/Wolfmanlq/p/3915978.html
Copyright © 2011-2022 走看看