Serialization between object classes and xml, and xml serialization of object classes
Reference page:
Http://www.yuanjiaocheng.net/CSharp/csharp-interface.html
Http://www.yuanjiaocheng.net/CSharp/Csharp-operators.html
Http://www.yuanjiaocheng.net/CSharp/Csharp-if-else.html
Http://www.yuanjiaocheng.net/CSharp/Csharp-ternary-operator.html
Http://www.yuanjiaocheng.net/CSharp/Cshartp-switch.html
/// <Summary> /// Xml serialization and deserialization // </summary> public class XmlUtil {public static string GetRoot (string xml) {if (xml = null) xml = ""; // prevent the following Replace () and Trim () errors: 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 object Deserialize (Type type, string xml) {if (xml = null) xml = ""; // prevent the following Replace () and Trim () errors: 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> // reverse Serialization /// </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 = "type"> 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: x in attribute. Sd XmlWriterSettings xmlWriterSettings = new XmlWriterSettings (); xmlWriterSettings. encoding = new UTF8Encoding (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}