Serialization between object classes and xml, and xml serialization of object classes
/// <Summary> /// Xml serialization and deserialization // </summary> public class XmlUtil {public static string GetRoot (string xml) {XmlDocument doc = new XmlDocument (); doc. loadXml (xml. replace ("\ r \ n ",""). replace ("\ 0 ",""). trim (); var e = doc. documentElement; return e. innerText ;} # region deserialization // <summary> // deserialization // </summary> // <param name = "xml"> XML string </param>/ // <returns> </returns> public static T Deserialize <T> (string xml) {return (T) Deserialize (typeof (T), xml );} /// <summary> /// deserialization /// </summary> /// <param name = "stream"> byte stream </param> /// <returns> </returns> public static T Deserialize <T> (Stream stream) {return (T) Deserialize (typeof (T), stream );} /// <summary> /// deserialization /// </summary> /// <param name = "type"> type </param> /// <param name = "xml"> XML string </param> // <returns> </returns> public static obj Ect Deserialize (Type type, string xml) {try {xml = xml. replace ("\ r \ n ",""). replace ("\ 0 ",""). trim (); using (StringReader sr = new StringReader (xml) {XmlSerializer xmldes = new XmlSerializer (type); return xmldes. deserialize (sr) ;}} catch (Exception e) {return null ;}} /// <summary> /// deserialization /// </summary> /// <param name = "type"> </param> /// <param name = "xml"> </param> // <returns> </returns> Public static object Deserialize (Type type, Stream stream) {XmlSerializer xmldes = new XmlSerializer (type); return xmldes. deserialize (stream );} # endregion # region serialization // <summary> // serialization // </summary> // <param name = "obj"> Object </param> // /<returns> </returns> public static string Serializer <T> (T obj) {return Serializer (typeof (T), obj);} // <summary> // serialization // </summary> // <param name = "ty Pe "> Type </param> /// <param name =" obj "> Object </param> /// <returns> </returns> public static string Serializer (Type type, object obj) {MemoryStream Stream = new MemoryStream (); XmlSerializerNamespaces _ name = new XmlSerializerNamespaces (); _ name. add ("", ""); // remove the xmlns: xsi and xmlns: xsd XmlWriterSettings xmlWriterSettings = new XmlWriterSettings (); xmlWriterSettings In the attribute. encoding = new UTF8Enc Oding (false); // sets the Encoding. Encoding cannot be used. UTF8 will cause xmlWriterSettings to be marked with BOM. indent = true; // set Automatic Indent // xmlWriterSettings. omitXmlDeclaration = true; // Delete XmlDeclaration: <? Xml version = "1.0" encoding = "UTF-16"?> // XmlWriterSettings. newLineChars = "\ r \ n"; // xmlWriterSettings. newLineHandling = NewLineHandling. none; XmlSerializer xml = new XmlSerializer (type); try {using (XmlWriter xmlWriter = XmlWriter. create (Stream, xmlWriterSettings) {// serialize the object xml. serialize (xmlWriter, obj, _ name) ;}} catch (InvalidOperationException) {throw;} return Encoding. UTF8.GetString (Stream. toArray ()). trim () ;}# endregion}