C # serialization,
Serialization is the process of converting the object state to a format that can be maintained or transmitted. In contrast to serialization, deserialization converts a stream into an object. These two processes can be combined to easily store and transmit data.
Using System; using System. IO; using System. text; using System. xml; using System. xml. serialization; // using System. runtime. serialization. json; using System. runtime. serialization. formatters. soap; using System. runtime. serialization. formatters. binary; namespace DotNet. utilities {public class SerializeHelper {public SerializeHelper () {}# region XML serialization // <summary> // file-based XML serialization // </summary> // <param nam E = "obj"> object </param> // <param name = "filename"> file path </param> public static void Save (object obj, string filename) {FileStream fs = null; try {fs = new FileStream (filename, FileMode. create, FileAccess. write, FileShare. readWrite); XmlSerializer serializer = new XmlSerializer (obj. getType (); serializer. serialize (fs, obj);} catch (Exception ex) {throw ex;} finally {if (fs! = Null) fs. close ();}} /// <summary> /// file-based XML deserialization // </summary> /// <param name = "type"> Object type </param> /// <param name = "filename"> file path </param> public static object Load (Type type, string filename) {FileStream fs = null; try {fs = new FileStream (filename, FileMode. open, FileAccess. read, FileShare. readWrite); XmlSerializer serializer = new XmlSerializer (type); return serializer. deserialize (fs );} Catch (Exception ex) {throw ex;} finally {if (fs! = Null) fs. close ();}} /// <summary> /// serialize XML serialization // </summary> /// <param name = "item"> Object </param> public string ToXml <T> (T item) {XmlSerializer serializer = new XmlSerializer (item. getType (); StringBuilder sb = new StringBuilder (); using (XmlWriter writer = XmlWriter. create (sb) {serializer. serialize (writer, item); return sb. toString ();}} /// <summary> /// deserialize XML /// </summary> /// <param name = "str"> string sequence </param> public T FromXml <T> (string str) {XmlSerializer serializer = new XmlSerializer (typeof (T); using (XmlReader reader = new XmlTextReader (new StringReader (str) {return (T) serializer. deserialize (reader );}} # endregion # region Json serialization // <summary> // JsonSerializer serialization /// </summary> /// <param name = "item"> Object </param> // public string ToJson <T> (T item) /// {// DataContractJsonSerializer serializer = new DataContractJsonSerializer (item. getType (); // using (MemoryStream MS = new MemoryStream () // {// serializer. writeObject (MS, item); // return Encoding. UTF8.GetString (ms. toArray ()); ///} // <summary> /// JsonSerializer deserialization //// </summary> //// <param name = "str"> string sequence </param> // public T FromJson <T> (string str) where T: class // {// DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (T); // using (MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (str) // {// return serializer. readObject (MS) as T; ///} // # endregion # region SoapFormatter serialization // <summary> // SoapFormatter serialization // </summary> // <param name = "item"> Object </param> public string ToSoap <T> (T item) {SoapFormatter formatter = new SoapFormatter (); using (MemoryStream MS = new MemoryStream () {formatter. serialize (ms, item); ms. position = 0; XmlDocument xmlDoc = new XmlDocument (); xmlDoc. load (MS); return xmlDoc. innerXml ;}} /// <summary> /// SoapFormatter deserialization /// </summary> /// <param name = "str"> string sequence </param> public T FromSoap <t> (string str) {XmlDocument xmlDoc = new XmlDocument (); xmlDoc. loadXml (str); SoapFormatter formatter = new SoapFormatter (); using (MemoryStream MS = new MemoryStream () {xmlDoc. save (ms); ms. position = 0; return (T) formatter. deserialize (MS );}} # endregion # region BinaryFormatter serialization // <summary> // BinaryFormatter serialization // </summary> // <param name = "item"> Object </param> public string ToBinary <T> (T item) {BinaryFormatter formatter = new BinaryFormatter (); using (MemoryStream MS = new MemoryStream () {formatter. serialize (ms, item); ms. position = 0; byte [] bytes = ms. toArray (); StringBuilder sb = new StringBuilder (); foreach (byte bt in bytes) {sb. append (string. format ("{0: X2}", bt);} return sb. toString ();}} /// <summary> /// BinaryFormatter deserialization /// </summary> /// <param name = "str"> string sequence </param> public T FromBinary <t> (string str) {int intLen = str. length/2; byte [] bytes = new byte [intLen]; for (int I = 0; I <intLen; I ++) {int ibyte = Convert. toInt32 (str. substring (I * 2, 2), 16); bytes [I] = (byte) ibyte;} BinaryFormatter formatter = new BinaryFormatter (); using (MemoryStream MS = new MemoryStream (bytes) {return (T) formatter. deserialize (MS); }}# endregion }}
SerializeHelper