Summary of C # serialization

Source: Internet
Author: User

Post your own serialized code:

  Public classXmlutil {/// <summary>        ///XML & DataContract Serialize & Deserialize Helper/// </summary>        /// <typeparam name= "T" ></typeparam>        /// <param name= "Serialobject" ></param>        /// <returns></returns>         Public Static stringSerializer<t> (T serialobject)whereT:class        {            stringresult =string.            Empty; using(MemoryStream mem =NewMemoryStream ()) {                using(XmlTextWriter writer =NewXmlTextWriter (Mem, Encoding.UTF8)) {System.Xml.Serialization.XmlSerializer Ser=NewSystem.Xml.Serialization.XmlSerializer (typeof(T)); Ser.                    Serialize (writer, serialobject); Result=Encoding.UTF8.GetString (Mem.                ToArray ()); }            }            returnresult; }         Public StaticT deserialize<t> (stringStrwhereT:class{T result=NULL; using(MemoryStream MemoryStream =NewMemoryStream (Encoding.UTF8.GetBytes (str))) {                using(StreamReader StreamReader =NewStreamReader (MemoryStream)) {System.Xml.Serialization.XmlSerializer XmlSerializer=NewSystem.Xml.Serialization.XmlSerializer (typeof(T)); Result=(T) xmlserializer.deserialize (MemoryStream); }            }            returnresult; }    }

The above-written continuous serialization will not have memory overflow performance issues, has been told to directly refer to the company of an old bird packaged dll to serialize, and later found that there is always a memory overflow, paste its wrong wording, only for the lesson:

 Public classXmlutil {/// <summary>        ///XML & DataContract Serialize & Deserialize Helper/// </summary>        /// <typeparam name= "T" ></typeparam>        /// <param name= "Serialobject" ></param>        /// <returns></returns>         Public Static stringSerializer<t> (T serialobject)whereT:class        {            //Try//{XmlSerializer ser =NewXmlSerializer (typeof(T)); System.IO.MemoryStream Mem=NewMemoryStream (); XmlTextWriter writer=NewXmlTextWriter (Mem, Encoding.UTF8); Ser.            Serialize (writer, serialobject); Writer.            Close (); returnEncoding.UTF8.GetString (Mem.            ToArray ()); //}            //catch (Exception ex)//{            //return null; //}        }         Public StaticT deserialize<t> (stringStrwhereT:class        {            //Try//{XmlSerializer Myserializer =NewXmlSerializer (typeof(T)); StreamReader mem2=NewStreamReader (NewMemoryStream (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); //}}

Ah, the veteran of the time to write code is so casual? See the tried catch that was commented out I guess he used to think there was something wrong here, but he didn't find it. Directly put in memory, flow is not released Ah ...

Summary of C # serialization

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.