Summary of use of serialization in C #

Source: Internet
Author: User
Post your own serialized code:

public class Xmlutil {//<summary>//XML & DataContract Serialize & Deserialize Helper </summary>//<typeparam name= "T" ></typeparam>//<param name= "Serialobjec T "></param>///<returns></returns> public static string serializer<t> (t Serialob ject) 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.Seria Lization.                    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 (Strea Mreader StreamReader = new StreamReader (MemoryStream)) {System.Xml.Serialization.XmlSer                    Ializer XmlSerializer = new System.Xml.Serialization.XmlSerializer (typeof (T));                result = (T) xmlserializer.deserialize (MemoryStream);        }} return result; }    }

The above notation will not have memory overflow performance problem, has been told to refer directly to the company of an old bird packaged dll to serialize, and later found that there is always a memory overflow, paste the wrong way of writing, only for lessons learned:

public class Xmlutil {//<summary>//XML & DataContract Serialize & Deserialize Helper </summary>//<typeparam name= "T" ></typeparam>//<param name= "Serialobjec T "></param>///<returns></returns> public static string serializer<t> (t Serialob ject) 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.js On.        Jsonconvert.serializeobject (value); }//public Static Object Json_deserializeobject (string value)//{//Return Newtonsoft.Json.Json        Convert.deserializeobject (value); }//public static T json_deserializeobject<t> (string value)//{//Return Newtonsoft.json.        Jsonconvert.deserializeobject<t> (value); //}    }
Related Article

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.