C # object class serialization and deserialization II (DataContractSerializer)
Public class PubXmlSerializer
{///
/// Serialize an object to an xml string //////
///
Public static string ToXMLString (T meta) {DataContractSerializer dcs = new DataContractSerializer (typeof (ActionInfo); string xmlStr = string. empty; using (MemoryStream MS = new MemoryStream () {try {dcs. writeObject (MS, meta); xmlStr = Encoding. UTF8.GetString (ms. toArray (); ms. close ();} catch (Exception ee) {MessageBox. show (ee. toString () ;}} return xmlStr ;}///
/// Object deserialization //////
///
Public static T ReadFromXML (string xmlStr) {T meta; DataContractSerializer dcs = new DataContractSerializer (typeof (ActionInfo); using (TextReader reader = new StringReader (xmlStr )) {using (XmlReader xReader = XmlReader. create (reader) {meta = (T) dcs. readObject (xReader) ;}} return meta ;}}