Using System;
Using System. IO;
Using System. Xml. Serialization;
Using System. Xml;
Using System. Collections. Generic;
Using System. Text;
Namespace Discuz. Common
{
/// <Summary>
/// Serialize the base class.
/// </Summary>
Public class SerializationHelper
{
Private SerializationHelper ()
{}
Private static Dictionary <int, XmlSerializer> serializer_dict = new Dictionary <int, XmlSerializer> ();
Public static XmlSerializer GetSerializer (Type t)
{
Int type_hash = t. GetHashCode ();
If (! Serializer_dict.ContainsKey (type_hash ))
Serializer_dict.Add (type_hash, new XmlSerializer (t ));
Return serializer_dict [type_hash];
}
/// <Summary>
/// Deserialization
/// </Summary>
/// <Param name = "type"> Object type </param>
/// <Param name = "filename"> file path </param>
/// <Returns> </returns>
Public static object Load (Type type, string filename)
{
FileStream fs = null;
Try
{
// Open the stream...
Fs = new FileStream (filename, FileMode. Open, FileAccess. Read, FileShare. ReadWrite );
XmlSerializer serializer = new XmlSerializer (type );
Return serializer. Deserialize (fs );
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
If (fs! = Null)
Fs. Close ();
}
}
/// <Summary>
/// Serialization
/// </Summary>
/// <Param name = "obj"> Object </param>
/// <Param name = "filename"> file path </param>
Public static bool Save (object obj, string filename)
{
Bool success = false;
FileStream fs = null;
// Serialize it...
Try
{
Fs = new FileStream (filename, FileMode. Create, FileAccess. Write, FileShare. ReadWrite );
XmlSerializer serializer = new XmlSerializer (obj. GetType ());
Serializer. Serialize (fs, obj );
Success = true;
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
If (fs! = Null)
Fs. Close ();
}
Return success;
}
/// <Summary>
/// Serialize xml into a string
/// </Summary>
/// <Param name = "obj"> Object </param>
/// <Returns> xml string </returns>
Public static string Serialize (object obj)
{
String returnStr = "";
XmlSerializer serializer = GetSerializer (obj. GetType ());
MemoryStream MS = new MemoryStream ();
XmlTextWriter xtw = null;
StreamReader sr = null;
Try
{
Xtw = new System. Xml. XmlTextWriter (MS, Encoding. UTF8 );
Xtw. Formatting = System. Xml. Formatting. Indented;
Serializer. Serialize (xtw, obj );
Ms. Seek (0, SeekOrigin. Begin );
Sr = new StreamReader (MS );
ReturnStr = sr. ReadToEnd ();
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
If (xtw! = Null)
Xtw. Close ();
If (sr! = Null)
Sr. Close ();
Ms. Close ();
}
Return returnStr;
}
Public static object DeSerialize (Type type, string s)
{
Byte [] B = System. Text. Encoding. UTF8.GetBytes (s );
Try
{
XmlSerializer serializer = GetSerializer (type );
Return serializer. Deserialize (new MemoryStream (B ));
}
Catch (Exception ex)
{
Throw ex;
}
}
}
}
Call
Protected void Button4_Click (object sender, EventArgs e)
{
List <book> books = new List <book> ();
Book bk = new book ();
Bk. Author = "AAA ";
Bk. BookName = "aaaa ";
Bk. Date = "1990 ";
Books. Add (bk );
Bk = new book ();
Bk. Author = "BBB ";
Bk. BookName = "bbb ";
Bk. Date = "1991 ";
Books. Add (bk );
Bk = new book ();
Bk. Author = "ccc ";
Bk. BookName = "ccc ";
Bk. Date = "1992 ";
Books. Add (bk );
SerializationHelper. Save (books, Server. MapPath ("aa. xml "));
}
Protected void Button5_Click (object sender, EventArgs e)
{
List <book> books = new List <book> ();
GridView1.DataSource = (List <book>) SerializationHelper. Load (books. GetType (), Server. MapPath ("aa. xml "));
GridView1.DataBind ();
}