Link: http://www.cnblogs.com/ejiyuan/archive/2009/01/21/1379256.html
The main functions of serialization and deserialization are:
1Read the information of the last saved object at the next startup of the process.
2In differentAppdomainOr transfer data between processes
3Transfer Data in Distributed Application Systems
......
InC #There are also three common serialization methods:Binaryformatter,Soapformatter,XMLSerialization
This article uses a small example to illustrate the specific usage and similarities of the three methods.
This example uses three different methodsBookObject serialization and deserialization, of course, thisBookClass can be serialized first.
BookClass
[Copy to clipboard]
Code:
Using system;
Using system. collections;
Using system. text;
Namespace serializabletest
{
[Serializable]
Public class Book
{
Public book ()
{
Albookreader = new arraylist ();
}
Public String strbookname;
[Nonserialized]
Public String strbookpwd;
Private string _ bookid;
Public String bookid
{
Get {return _ bookid ;}
Set {_ bookid = value ;}
}
Public arraylist albookreader;
Private string _ bookprice;
Public void setbookprice (string price)
{
_ Bookprice = price;
}
Public void write ()
{
Console. writeline ("book id:" + bookid );
Console. writeline ("book name:" + strbookname );
Console. writeline ("book password:" + strbookpwd );
Console. writeline ("book price:" + _ bookprice );
Console. writeline ("book reader :");
For (INT I = 0; I <albookreader. Count; I ++)
{
Console. writeline (albookreader [I]);
}
}
}
}
This class is relatively simple, that is, somePublicField and a read/write attribute,PrivateField, marked[Nonserialized]In the following example.
I,BinaryformatterSerialization Method
1Serialization meansBookClass assignment, and then serialized to a file
[Copy to clipboard]
Code:
book = New Book ();
book. bookid = "1";
book. albookreader. add ("gspring");
book. albookreader. add (" Yongchun ");
book. strbookname = "C # enhanced ";
book. strbookpwd = "*****";
book. setbookprice ("50.00");
binaryserialize serialize = new binaryserialize ();
serialize. serialize (book);
2, Deserialization
[Copy to clipboard]
Code:
Binaryserialize serialize = new binaryserialize ();
Book = serialize. deserialize ();
Book. Write ();
3, For testing
BinaryserializeClass
[Copy to clipboard]
Code:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. runtime. serialization. formatters. Binary;
Namespace serializabletest
{
Public class binaryserialize
{
String strfile = "C:" book. Data ";
Public void serialize (Book)
{< br> using (filestream FS = new filestream (strfile, filemode. create)
{< br> binaryformatter formatter = new binaryformatter ();
formatter. serialize (FS, book);
}< BR >}
Public book deserialize ()
{
Book;
Using (filestream FS = new filestream (strfile, filemode. Open ))
{
Binaryformatter formatter = new binaryformatter ();
Book = (book) formatter. deserialize (FS );
}
Return book;
}
}
}
Mainly calledSystem. runtime. serialization. formatters. BinarySpaceBinaryformatterClass for serialization and deserialization, written to a file in a thumbnail binary format, the speed is relatively fast, and the written file has been saved in binary format has a certain confidentiality effect.
After deserialization is called:
In other wordsNonserializedAll other members can be serialized.
II,SoapformatterSerialization Method
The methods for calling serialization and deserialization are similar to those above. I will not list them. Let's take a look at them.SoapserializeClass
SoapserializeClass
[Copy to clipboard]
Code:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. runtime. serialization. formatters. Soap;
Namespace serializabletest
{
Public class soapserialize
{
String strfile = "C:" "book. Soap ";
Public void serialize (Book)
{
Using (filestream FS = new filestream (strfile, filemode. Create ))
{
Soapformatter formatter = new soapformatter ();
formatter. serialize (FS, book);
}< BR >}
Public book deserialize ()
{
Book;
Using (filestream FS = new filestream (strfile, filemode. Open ))
{
Soapformatter formatter = new soapformatter ();
Book = (book) formatter. deserialize (FS );
}
Return book;
}
}
}
Mainly called System. runtime. serialization. formatters. Soap Space Soapformatter Class for serialization and deserialization, which must be applied before use System. runtime. serialization. formatters. Soap. dll ( . Net Built-in)
The serialized file is Soap Format File ( Simple Object Access Protocol ( Simple Object Access Protocol , Soap Is a lightweight, simple, and XML The Protocol, which is designed in Web Exchange structured and solidified information. Soap Can be used in combination with many existing Internet protocols and formats, including Hypertext Transfer Protocol ( HTTP ), Simple mail Transmission Protocol ( SMTP ), Multi-purpose Internet Mail Extension protocol ( Mime ). It also supports calling from the message system to the remote process ( RPC ).Program. Soap Use XML Data Structure and hyper-text transfer protocol (HTTP) Defines a standard method to use. Internet Distributed Objects in different operating environments. )
The result after deserialization is called is the same as method 1.
III,XMLSerialization Method
The methods for calling serialization and deserialization are similar to those above. I will not list them. Let's take a look at them.XmlserializeClass
XmlserializeClass
[Copy to clipboard]
Code:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. xml. serialization;
Namespace serializabletest
{
Public class xmlserialize
{
String strfile = "C:" "book. xml ";
Public void serialize (Book)
{< br> using (filestream FS = new filestream (strfile, filemode. create)
{< br> xmlserializer formatter = new xmlserializer (typeof (book);
formatter. serialize (FS, book);
}< BR >}
Public book deserialize ()
{
Book;
Using (filestream FS = new filestream (strfile, filemode. Open ))
{
Xmlserializer formatter = new xmlserializer (typeof (book ));
Book = (book) formatter. deserialize (FS );
}
Return book;
}
}
}
From the three test classes, we can see that the calling methods of the three methods are similar, but the classes used are different.
XMLThe serialized file is a commonXMLFile:
Book. xml
[Copy to clipboard]
Code:
<? XML version = "1.0"?>
<Book xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema">
<Strbookname> C #Enhancement</Strbookname>
<Strbookpwd> ***** </strbookpwd>
<Albookreader>
<Anytype xsi: TYPE = "XSD: string"> gspring </anytype>
<Anytype xsi: TYPE = "XSD: string">Yongchun</Anytype>
</Albookreader>
<Bookid> 1 </bookid>
</Book>
The output is as follows:
That is to say, useXMLThe serialization method can only be saved.PublicForPrivateFields of other types cannot be serialized.
Circular references:
For example, in the preceding exampleBookAdd the following attribute to the class:
Public book relationbook;
Use the following method when calling serialization:
[Copy to clipboard]
Code:
book = New Book ();
book. bookid = "1";
book. albookreader. add ("gspring");
book. albookreader. add (" Yongchun ");
book. strbookname = "C # enhanced ";
book. strbookpwd = "*****";
book. setbookprices ("50.00");
book book2 = New Book ();
book2.bookid = "2";
book2.albookreader. add ("gspring");
book2.albookreader. add (" Yongchun ");
book2.strbookname = ". net enhancement ";
book2.strbookpwd = "*****";
book2.setbookprice ("40.00");
Book. relationbook = book2;
Book2.relationbook = book;
Binaryserialize serialize = new binaryserialize ();
Serialize. serialize (book );
In this case, the circular references will occur.BinaryserializeAndSoapserializeNormal serialization(. NETInternally processed),XmlserializeIn this case, an error is reported:"Serialization typeSerializabletest. BookThe circular references are detected."