C # serialization summary,

Source: Internet
Author: User

C # serialization summary,

Paste your own serialized code:

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

The above method of continuous serialization does not have the performance problem of memory overflow. Previously, it was told to directly reference a dll encapsulated by the company's old bird for serialization. Later, it was found that there was always a memory overflow, paste the incorrect Writing Method to learn the following lessons:

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

Hey, is it so casual to write code when laruence is in a hurry? Seeing the commented-out try catch, I guess he once thought there was a problem, but I still didn't find it. Directly put it in the memory, and the stream will not be released...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.