C # serialization deserialization help class

Source: Internet
Author: User

 

// Reprinted: http://hi.baidu.com/fxh19860822/blog/item/df35230b3ded441495ca6bd5.html
In C #, there are also three common serialization Methods: BinaryFormatter, SoapFormatter, and XML serialization.

/// <Summary>
/// Provides static methods related to serialization and deserialization objects.
/// </Summary>
Public class SerializerHelper
{
/// <Summary>
/// Serialize the specified object to an XML or binary file and return the execution status.
/// </Summary>
/// <Param name = "o"> object to be serialized </param>
/// <Param name = "path"> Save path </param>
/// <Param name = "isBinaryFile"> whether the serialized file type is a binary file; true indicates a binary file; otherwise, it is an xml file or a text file. </param>
/// <Returns> return the execution status </returns>
Public static bool Serialize (Object o, string path, bool isBinaryFile)
{
Bool flag = false;
Try
{
If (isBinaryFile)
{
BinaryFormatter formatter = new BinaryFormatter ();
Using (FileStream stream = new FileStream (path, FileMode. Create ))
{
Formatter. Serialize (stream, o );
Flag = true;
}
}
Else
{
XmlSerializer serializer = new XmlSerializer (o. GetType ());
Using (XmlTextWriter writer = new XmlTextWriter (path, Encoding. UTF8 ))
{
Writer. Formatting = Formatting. Indented;
XmlSerializerNamespaces n = new XmlSerializerNamespaces ();
N. Add ("","");
Serializer. Serialize (writer, o, n );
Flag = true;
}
}
}
Catch {flag = false ;}
Return flag;
}

/// <Summary>
/// Serialize the specified object to a string in XML format and return it.
/// </Summary>
/// <Param name = "o"> object to be serialized </param>
/// <Returns> returns the serialized string </returns>
Public static string Serialize (Object o)
{
String xml = "";
Try
{
XmlSerializer serializer = new XmlSerializer (o. GetType ());
Using (MemoryStream mem = new MemoryStream ())
{
Using (XmlTextWriter writer = new XmlTextWriter (mem, Encoding. UTF8 ))
{
Writer. Formatting = Formatting. Indented;
XmlSerializerNamespaces n = new XmlSerializerNamespaces ();
N. Add ("","");
Serializer. Serialize (writer, o, n );

Mem. Seek (0, SeekOrigin. Begin );
Using (StreamReader reader = new StreamReader (mem ))
{
Xml = reader. ReadToEnd ();
}
}
}
}
Catch {xml = "";}
Return xml;
}

/// <Summary>
/// Deserialize the corresponding object from the specified file and return it.
/// </Summary>
/// <Param name = "t"> object type to be deserialized </param>
/// <Param name = "path"> file path </param>
/// <Param name = "isBinaryFile"> whether the deserialization file type is binary, true is binary, otherwise it is an xml file or a text file </param>
/// <Returns> return Object </returns>
Public static object Deserialize (Type t, string path, bool isBinaryFile)
{
Object o = null;
Try
{
If (! IsBinaryFile)
{
XmlSerializer serializer = new XmlSerializer (t );
Using (XmlTextReader reader = new XmlTextReader (path ))
{
O = serializer. Deserialize (reader );
}
}
Else
{
BinaryFormatter formatter = new BinaryFormatter ();
Using (FileStream stream = new FileStream (path, FileMode. Open, FileAccess. Read ))
{
O = formatter. Deserialize (stream );
}
}
}
Catch {o = null ;}
Return o;
}

/// <Summary>
/// Deserialize characters in the specified xml format into corresponding objects and return them.
/// </Summary>
/// <Param name = "t"> Object Type </param>
/// <Param name = "xml"> content of characters in xml format to be deserialized </param>
/// <Returns> returns the corresponding object </returns>
Public static Object Deserialize (Type t, string xml)
{
Object o = null;
Try
{
XmlSerializer serializer = new XmlSerializer (t );
Using (MemoryStream mem = new MemoryStream (Encoding. UTF8.GetBytes (xml )))
{
O = serializer. Deserialize (mem );
}
}
Catch {o = null ;}
Return o;
}

/// <Summary>
/// Serialize the specified object to an XML file and return the execution status.
/// </Summary>
/// <Param name = "o"> object to be serialized </param>
/// <Param name = "path"> name of the generated file </param>
/// <Returns> return the execution status </returns>
Public static bool XmlSerialize (Object o, string path)
{
Return SerializerHelper. Serialize (o, path, false );
}

/// <Summary>
/// Deserializes the specified XML file into the corresponding object and returns it.
/// </Summary>
/// <Param name = "t"> Object Type </param>
/// <Param name = "path"> XML file path </param>
/// <Returns> returned object </returns>
Public static Object XmlDeserialize (Type t, string path)
{
Return SerializerHelper. Deserialize (t, path, false );
}

/// <Summary>
/// Serialize the specified object to a binary file and return the execution status.
/// </Summary>
/// <Param name = "o"> object to be serialized </param>
/// <Param name = "path"> name of the generated file </param>
/// <Returns> return the execution status </returns>
Public static bool BinarySerialize (Object o, string path)
{
Return SerializerHelper. Serialize (o, path, true );
}

/// <Summary>
/// Deserializes the specified binary file into the corresponding object and returns it.
/// </Summary>
/// <Param name = "t"> Object Type </param>
/// <Param name = "path"> XML file path </param>
/// <Returns> returned object </returns>
Public static Object BinaryDeserialize (Type t, string path)
{
Return SerializerHelper. Deserialize (t, path, true );
}
}
Http://hi.baidu.com/fxh19860822/blog/item/381f1cd20daf3a3f970a16db.html

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.