C # entity class serialization and deserialization two (DataContractSerializer)

Source: Internet
Author: User
Using system;using system.collections.generic;using system.linq;using system.text;using System.runtime.serialization;using system.io;using system.xml;namespace common.utility.xmlserialization{//< summary>//DataContractSerializer//</summary>//<typeparam name= "T" ></typeparam> p Ublic class Datacontractserializer<t> {//<summary>//entity serialized as XML string///&LT;/SU mmary>//<param name= "meta" ></param>///<returns></returns> public stat            IC string toxmlstring (t meta) {DataContractSerializer DCS = new DataContractSerializer (typeof (T)); String xmlstr = String.            Empty; using (MemoryStream ms = new MemoryStream ()) {try {DCs.                    WriteObject (MS, Meta); Xmlstr = Encoding.UTF8.GetString (ms.                    ToArray ()); Ms.                Close ();      }          catch {throw;        }} return xmlstr; }///<summary>//DataContract serialization///</summary>/<param name= "value" >&        lt;/param>//<param name= "KnownTypes" > collection of known Types </param>//<returns></returns>            public static string Toxmlstring (object value, ienumerable<type> knownTypes = null) {try {DataContractSerializer DataContractSerializer = new DataContractSerializer (value.                GetType (), knownTypes); using (MemoryStream ms = new MemoryStream ()) {Datacontractserializer.writeobject (MS, VA                    Lue); Ms.                    Seek (0, Seekorigin.begin); using (StreamReader sr = new StreamReader (ms)) {return Sr.                    ReadToEnd ();      }                }            }      catch {throw; }}////<summary> Entity deserialization///</summary>//<param name= "xml" >&lt            ;/param>//<returns></returns> public static T Readfromxml (string xmlstr) {            T Meta;                try {DataContractSerializer DCs = new DataContractSerializer (typeof (T)); using (TextReader reader = new StringReader (XMLSTR)) {using (XmlReader Xreader = Xmlrea Der. Create (reader)) {meta = (T) DCs.                    ReadObject (Xreader);            }}} catch {throw;        } return meta; }///<summary> Entity deserialization///</summary>//<param name= "Xmlstr" ></para m>//<param name= "KnownTypes" >IEnumerable<Type></param>//<returns></returns> public static T Readfromxml (string xmlstr, ienumerable&lt ;            Type> knownTypes = null) {T meta;                try {DataContractSerializer DCs = new DataContractSerializer (typeof (T), knownTypes); using (TextReader reader = new StringReader (XMLSTR)) {using (XmlReader Xreader = Xmlreader.create (reader)) {meta = (T) DCs.                    ReadObject (Xreader);            }}} catch {throw;        } return meta; }    }}


The above is the C # entity class serialization and deserialization two (DataContractSerializer) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.