Serialization between object classes and xml, and xml serialization of object classes

Source: Internet
Author: User

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}

 

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.