Public class serializablehelper
{
/// <Summary>
/// Serialize to a byte array
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "T"> </param>
/// <Returns> </returns>
Public static byte [] serializetobytes <t> (T)
{
Memorystream mstream = new memorystream ();
Binaryformatter SER = new binaryformatter ();
Ser. serialize (mstream, t );
Return mstream. toarray ();
}
/// <Summary>
/// Serialize to a string
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "T"> </param>
/// <Returns> serialization Code </returns>
Public static string serializetoxml <t> (T)
{
Try
{
Xmlserializer S = new xmlserializer (typeof (t ));
Stream stream = new memorystream ();
S. serialize (stream, t );
Stream. Seek (0, seekorigin. Begin); // This is very important; otherwise, it cannot be read.
String strsource = "";
Using (streamreader reader = new streamreader (Stream ))
{
Strsource = reader. readtoend ();
}
Return strsource;
}
Catch {return NULL ;}
}
/// <Summary>
/// Deserialize the XML file into a Generic Array
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "path"> </param>
/// <Returns> </returns>
Public static t deserialize <t> (fileinfo FI)
{
If (Fi. exists = false) return default (t );
Xmlserializer = new xmlserializer (typeof (t ));
Filestream FS = Fi. openread ();
T;
Try
{
T = (t) xmlserializer. deserialize (FS );
}
Finally
{
FS. Close ();
}
Return T;
}
/// <Summary>
/// String deserialization into a class
/// </Summary>
/// <Param name = "binary"> </param>
/// <Returns> </returns>
Public static t deserialize <t> (string xmlsource)
{
If (string. isnullorempty (xmlsource) return default (t );
Try
{
Xmlserializer x = new xmlserializer (typeof (t ));
Stream stream = new memorystream (encoding. utf8.getbytes (xmlsource ));
Stream. Seek (0, seekorigin. Begin );
Object OBJ = x. deserialize (Stream );
Stream. Close ();
Return (t) OBJ;
}
Catch
{
Return default (t );
}
}
Public static dictionary <tkey, tvalue> deserialize <tkey, tvalue> (fileinfo FI)
{
If (Fi. exists = false) return default (Dictionary <tkey, tvalue> );
Filestream FS = Fi. openread ();
If (FS. Length = 0) return default (Dictionary <tkey, tvalue> );
Xmlreader reader = xmlreader. Create (FS );
Xmlserializer keyserializer = new xmlserializer (typeof (tkey ));
Xmlserializer valueserializer = new xmlserializer (typeof (tvalue ));
Bool wasempty = reader. isemptyelement;
If (wasempty)
Return default (Dictionary <tkey, tvalue> );
Dictionary <tkey, tvalue> DIC = new dictionary <tkey, tvalue> ();
While (reader. Read ())
{
If (reader. nodetype! = Xmlnodetype. element) continue;
If (reader. Name = "root") continue;
Reader. readstartelement ("row ");
Reader. readstartelement ("key ");
Tkey key = (tkey) keyserializer. deserialize (Reader );
Reader. readendelement ();
Reader. readstartelement ("value ");
Tvalue value = (tvalue) valueserializer. deserialize (Reader );
Reader. readendelement ();
Dic. Add (Key, value );
Reader. readendelement ();
// Reader. movetocontent ();
}
Return DIC;
}
/// <Summary>
/// Serialize the Generic Array as an XML file
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "T"> </param>
/// <Param name = "fullname"> </param>
/// <Returns> whether serialization is successful </returns>
Public static bool serialize <t> (T, string fullname)
{
Xmlserializer = new xmlserializer (typeof (t ));
Textwriter writer = new streamwriter (fullname );
Try
{
Xmlserializer. serialize (writer, t );
Return true;
}
Catch
{
Return false;
}
Finally
{
Writer. Close ();
}
}
/// <Summary>
/// Serialize the dictionary
/// </Summary>
/// <Typeparam name = "tkey"> </typeparam>
/// <Typeparam name = "tvalue"> </typeparam>
/// <Param name = "dic"> </param>
/// <Param name = "fullname"> </param>
/// <Returns> </returns>
Public static bool serialize <tkey, tvalue> (Dictionary <tkey, tvalue> DIC, string fullname)
{
Try
{
System. xml. xmlwritersettings settings = new system. xml. xmlwritersettings ();
Settings. Encoding = encoding. utf8;
Settings. indent = true;
Settings. conformancelevel = conformancelevel. Fragment;
Xmlwriter writer = xmlwriter. Create (fullname, settings );
Xmlserializer keyserializer = new xmlserializer (typeof (tkey ));
Xmlserializer valueserializer = new xmlserializer (typeof (tvalue ));
Writer. writestartelement ("root ");
Foreach (keyvaluepair <tkey, tvalue> kV in DIC)
{
Writer. writestartelement ("row ");
Writer. writestartelement ("key ");
Keyserializer. serialize (writer, Kv. Key );
Writer. writeendelement ();
Writer. writestartelement ("value ");
Valueserializer. serialize (writer, Kv. value );
Writer. writeendelement ();
Writer. writeendelement ();
} Writer. writeendelement ();
Writer. Close ();
Return true;
}
Catch
{
Try
{
File. Delete (fullname );
}
Catch {}
Return false;
}
}
/// <Summary>
/// Serialize the datatable
/// </Summary>
/// <Param name = "PDT"> datatable containing data </param>
/// <Returns> serialized able </returns>
Public static string serializedatatablexml (PDT able PDT)
{
// Serialize the datatable
Stringbuilder sb = new stringbuilder ();
Xmlwriter writer = xmlwriter. Create (SB );
Xmlserializer serializer = new xmlserializer (typeof (datatable ));
Serializer. serialize (writer, PDT );
Writer. Close ();
Return sb. tostring ();
}
/// <Summary>
/// Deserialize the able
/// </Summary>
/// <Param name = "pxml"> serialized datatable </param>
/// <Returns> datatable </returns>
Public static datatable deserializedatatable (string pxml)
{
Stringreader strreader = new stringreader (pxml );
Xmlreader = xmlreader. Create (strreader );
Xmlserializer serializer = new xmlserializer (typeof (datatable ));
Datatable dt = serializer. deserialize (xmlreader) as datatable;
Return DT;
}
}
From: http://www.cnblogs.com/xiaoye/articles/2125696.html