C#中序列化的使用總結

來源:互聯網
上載者:User
貼一下自己序列化的代碼:

public class XMLUtil    {        /// <summary>        /// XML & Datacontract Serialize & Deserialize Helper        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="serialObject"></param>        /// <returns></returns>        public static string Serializer<T>(T serialObject) where T : class        {            string result = string.Empty;            using (MemoryStream mem = new MemoryStream())            {                using (XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8))                {                    System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));                    ser.Serialize(writer, serialObject);                    result = Encoding.UTF8.GetString(mem.ToArray());                }            }            return result;        }        public static T Deserialize<T>(string str) where T : class        {            T result = null;            using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(str)))            {                using (StreamReader streamReader = new StreamReader(memoryStream))                {                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));                    result = (T)xmlSerializer.Deserialize(memoryStream);                }            }            return result;        }    }

上面的寫法持續序列化不會有記憶體溢出的效能問題,之前一直被告知直接引用公司某位老鳥封裝好的dll來序列化,後來發現了老是出現記憶體溢出,貼一下它的錯誤寫法,僅供吸取教訓:

public class XMLUtil    {        /// <summary>        /// XML & Datacontract Serialize & Deserialize Helper        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="serialObject"></param>        /// <returns></returns>        public static string Serializer<T>(T serialObject) where T : class        {            //try            //{            XmlSerializer ser = new XmlSerializer(typeof(T));            System.IO.MemoryStream mem = new MemoryStream();            XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8);            ser.Serialize(writer, serialObject);            writer.Close();            return Encoding.UTF8.GetString(mem.ToArray());            //}            //catch (Exception ex)            //{            //    return null;            //}        }        public static T Deserialize<T>(string str) where T : class        {            //try            //{            XmlSerializer mySerializer = new XmlSerializer(typeof(T));            StreamReader mem2 = new StreamReader(                    new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str)),                    System.Text.Encoding.UTF8);            return (T)mySerializer.Deserialize(mem2);            //}            //catch (Exception)            //{            //    return null;            //}        }        //public static string Json_SerializeObject(object value)        //{        //    return Newtonsoft.Json.JsonConvert.SerializeObject(value);        //}        //public static object Json_DeserializeObject(string value)        //{        //    return Newtonsoft.Json.JsonConvert.DeserializeObject(value);        //}        //public static T Json_DeserializeObject<T>(string value)        //{        //    return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(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.