Home  >  Article  >  Backend Development  >  Summary of the use of serialization in C#

Summary of the use of serialization in C#

黄舟
黄舟Original
2017-08-08 14:14:171299browse

Post your own serialization 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 writing method will not cause memory overflow performance problems for continuous serialization. I have been told to directly reference the dll encapsulated by a veteran in the company to serialize. , and later discovered that memory overflow always occurs, I will post the wrong way of writing it, just to learn from it:

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

The above is the detailed content of Summary of the use of serialization in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn