Serialization base class

Source: Internet
Author: User

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 ();

}

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.