C # serialize and deserialize)

Source: Internet
Author: User

Serialization, also known as serialization, is a mechanism used by the. NET runtime environment to support user-defined streaming. The objective is to make the custom object persistent by storage, or to transmit the object from one place to another.

The. NET Framework provides two serialization methods: 1. Using binaryformatter for serialization; 2. Using xmlserializer for serialization. The first method provides a simple binary data stream and some additional type information, while the second method formats the data stream for XML storage. Available[Serializable] attributeClass flag is serializable. If the elements of a class do not want to be serialized, 1. You can use[Nonserialized] attribute to mark, 2. can be used[Xmlignore.

1. binaryformatter

Namespace: system. runtime. serialization. formatters. Binary;
The following defines a serializable class:

 [Serializable]  //  Indicates that the class can be serialized.  
Class Peoson
{
Public Peoson (){}
Public Peoson ( String Name, Int Age)
{
This . Name = Name;
This . Age = Age;
}
Private String Name;
Public String Name
{
Get { Return Name ;}
Set {Name = Value ;}
}
[Nonserialized] // The following age field is not serialized.
Private Int Age;
Public Int Age
{
Get { Return Age ;}
Set {Age = Value ;}
}
Public Void Sayhi ()
{
Console. writeline ( " Hello everyone, I'm {0}, {1} years old. " , Name, age );
}
}

In the main function:

 List  < Peoson  >  Listpers  =     New  List  <  Peoson  >  ();
Peoson Per1 = New Peoson ( " Zhang San " , 18 );
Peoson per2 = New Peoson ( " Li Si " , 20 );
Listpers. Add (Per1 );
Listpers. Add (per2 );
Serializemethod (listpers );
// Reserializemethod (); // Deserialize to comment out the above line and execute this line
Console. readkey ();

Serialization and deserialization methods:

    Static     Void  Reserializemethod ()
{
// Deserialization
Using (Filestream FS = New Filestream ( " 1. Bin " , Filemode. Open ))
{
Binaryformatter BF = New Binaryformatter ();
List < Peoson > List = BF. deserialize (FS) As List < Peoson > ;
If (List ! = Null )
{

For ( Int I = 0 ; I < List. Count; I ++ )
{
List [I]. sayhi ();
}
}

}
}

Static Void Serializemethod (list < Peoson > Listpers)
{
// Serialization
Using (Filestream FS = New Filestream ( " 1. Bin " , Filemode. Create ))
{
Binaryformatter BF = New Binaryformatter ();
BF. serialize (FS, listpers );
}
}
}

Deserialization output result:

Hello everyone, I'm Zhang San, 0 years old.

Hello, everyone. I'm Li Si, I'm 0 years old.

From this we can see that the value of the unserialized field is null.

2. Use xmlserializer for serialization
There is another problem with the formatter. Suppose we need XML. There are two solutions: Either write a class that implements the iformatter interface, which is similar to the soapformatter class, but there is no information you don't need. either use the library class xmlserializer, this class does not use the serializable attribute, but it provides similar functions.
If we don't want to use the mainstream serialization mechanism, but want to use xmlseralizer for serialization, we need to modify it as follows:
A. Add the system. xml. serialization namespace.
B. the serializable and noserialized attributes are ignored, but the xmlignore attributes are used. Their behavior is similar to that of noserialized.
C. xmlseralizer requires the class to have a default constructor. This condition may already be met.
Serialization:

Xmlserializer xs = new xmlserializer (typeof (list <peoson> ));
Xs. serialize (FS, listpers );
Deserialization:

Xmlserializer xs = new xmlserializer (typeof (list <peoson> ));
List <peoson> List = Xs. deserialize (FS) as list <peoson>;

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.