Dictionary序列化和還原序列化

來源:互聯網
上載者:User

標籤:des   blog   os   io   for   ar   cti   div   

 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;        }    }

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.