<summary>///entity class serialization deserialization class////</summary>//<typeparam name= "T" ></typeparam> Public abstract class Entityclassxmlserializer<t> {//<summary>///entity class serialized into XML string </summary>//<param name= "entity" ></param>//<returns></returns> public static string toxmlstring (T entity) {using (MemoryStream stream = new MemoryStream ()) {XmlTextWriter writer = new XmlTextWriter (stream, NULL); XmlSerializer XML = new XmlSerializer (entity. GetType ()); Xml. Serialize (writer, entity); Writer. formatting = formatting.indented; using (StreamReader sr = new StreamReader (stream, System.Text.Encoding.UTF8)) {STREAM.P osition = 0; String xmlstring = Sr. ReadToEnd (); Sr. Close (); StreAm. Close (); return xmlstring; }}}///<summary> entity class deserialization////</summary>//<param Nam E= "xml" ></param>///<returns></returns> public static T readfromxml (string xml) {T entity; byte[] Byts = Encoding.UTF8.GetBytes (XML); using (MemoryStream stream = new MemoryStream (Byts)) {XmlSerializer xs = new XmlSerializer (type Of (T)); entity = (T) xs. Deserialize (stream); return entity; } } }
The above is the C # entity class serialization and deserialization one (XmlSerializer) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!