. Net serialization and deserialization

Source: Internet
Author: User

Original article: http://dotnet.mblogger.cn/cuijiazhao/posts/3400.aspx

. NET provides three serialization methods:

1. xml serialize

2. Soap serialize

3. Binary serialize

The first serialization method does not support serialization of some types, such as hashtable. I mainly introduce the serialization of the last two types.

I. Soap serialize

Use soapformatter. serialize () implements serialization. soapfamatter in system. runtime. serialization. formatters. in the soap namespace, system must be referenced for use. runtime. serialization. formatters. soap. DLL. it can serialize objects into XML.

[Serializable]
Public class option: iserializable
{
// This constructor must be implemented and called during deserialization.
Public option (serializationinfo Si, streamingcontext context)
{
This. _ name = Si. getstring ("name ");
This. _ text = Si. getstring ("text ");
This. _ ATT = (meteorattributecollection) Si. getvalue ("att_coll", typeof (meteorattributecollection ));
}
Public option () {_ ATT = new meteorattributecollection ();}
Public option (string name, string text, meteorattributecollection ATT)
{
_ Name = Name;
_ Text = text;
_ ATT = (ATT = NULL? New meteorattributecollection (): ATT );
}
Private string _ name;
Private string _ text;
Private meteorattributecollection _ ATT;
/// <Summary>
/// Node name
/// </Summary>
Public string name
{
Get {return this. _ name ;}
Set {This. _ name = value ;}
}
/// <Summary>
/// The node Text Value
/// </Summary>
Public String text
{
Get {return this. _ text ;}
Set {This. _ text = value ;}
}
/// <Summary>
/// Attributes of this node
/// </Summary>
Public meteorattributecollection attributelist
{
Get {return this. _ ATT ;}
Set {This. _ ATT = value ;}
}
/// This method must be implemented
Public Virtual void getobjectdata (serializationinfo info, streamingcontext context)
{
Info. addvalue ("name", this. _ name );
Info. addvalue ("text", this. _ text );
Info. addvalue ("att_coll", this. _ ATT, typeof (meteorattributecollection ));
}
}
In this class, the red part is required. otherwise, exceptions such as "must be marked as serializable" and "constructor of option type objects not found in deserialization" will occur during serialization.

*****************************

The following is the serialization and deserialization class meteorserializer. CS.

************************

Using system;
Using system. IO;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Soap;
Using system. runtime. serialization. formatters. Binary;

Namespace maxplatform. Grid
{
/// <Summary>
/// Provides methods for serializing objects (soapserializer, binaryserializer)
/// </Summary>
Public class meteorserializer
{
Public meteorserializer ()
{

}

# Region soap
/// <Summary>
/// Serialization in soap format
/// </Summary>
/// <Param name = "O"> </param>
/// <Returns> </returns>
Public static string soapserializer (Object O)
{
// To serialize the hashtable and its key/value pairs,
// You must first open a stream for writing.
// In this case, use a file stream.
// Filestream FS = new filestream ("datafile. xml", filemode. Create );
Stream MS = new memorystream ();
// Construct a binaryformatter and use it to serialize the data to the stream.
Soapformatter formatter = new soapformatter ();
Try
{
Formatter. serialize (MS, O );

Byte [] B = new byte [Ms. Length];
Ms. Position = 0;
Ms. Read (B, 0, B. Length );

String S = convert. tobase64string (B );
Return S;
}
Catch (serializationexception E)
{
// Log. Write ("servicenode: exception", "failed to serialize. Reason:" + E. Message );
Throw E;
}
Finally
{
Ms. Close ();
}

}
/// <Summary>
/// Deserialization in soap format
/// </Summary>
/// <Param name = "returnstring"> </param>
/// <Returns> </returns>
Public static object soapdeserialize (string returnstring)
{

// Open the file containing the data that you want to deserialize.
Soapformatter formatter;
Memorystream MS = NULL;
Try
{
Formatter = new soapformatter ();

Byte [] B = convert. frombase64string (returnstring );

MS = new memorystream (B );

// Deserialize the hashtable from the file and
// Assign the reference to the local variable.
Object o = formatter. deserialize (MS );
Return O;
}
Catch (serializationexception E)
{
// Log. Write ("servicenode: exception", "failed to deserialize. Reason:" + E. Message );
Throw E;
}
Finally
{
Ms. Close ();

}

}
# Endregion

# Region binary
/// <Summary>
/// Serialization in binary format
/// </Summary>
/// <Param name = "O"> </param>
/// <Returns> </returns>
Public static string binaryserializer (Object O)
{
// To serialize the hashtable and its key/value pairs,
// You must first open a stream for writing.
// In this case, use a file stream.
// Filestream FS = new filestream ("datafile. xml", filemode. Create );
Stream MS = new memorystream ();
// Construct a binaryformatter and use it to serialize the data to the stream.
Binaryformatter formatter = new binaryformatter ();
Try
{
Formatter. serialize (MS, O );

Byte [] B = new byte [Ms. Length];
Ms. Position = 0;
Ms. Read (B, 0, B. Length );

String S = convert. tobase64string (B );
Return S;
}
Catch (serializationexception E)
{
// Log. Write ("servicenode: exception", "failed to serialize. Reason:" + E. Message );
Throw E;
}
Finally
{
Ms. Close ();
}

}
/// <Summary>
/// Deserialization in binary format
/// </Summary>
/// <Param name = "returnstring"> </param>
/// <Returns> </returns>
Public static object binarydeserialize (string returnstring)
{

// Open the file containing the data that you want to deserialize.
Binaryformatter formatter;
Memorystream MS = NULL;
Try
{
Formatter = new binaryformatter ();

Byte [] B = convert. frombase64string (returnstring );

MS = new memorystream (B );

// Deserialize the hashtable from the file and
// Assign the reference to the local variable.
Object response = formatter. deserialize (MS );
Return response;
}
Catch (serializationexception E)
{
// Log. Write ("servicenode: exception", "failed to deserialize. Reason:" + E. Message );
Throw E;
}
Finally
{
Ms. Close ();

}

}
# Endregion

}
}

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.