20150317--serialization, deserialization (2)

Source: Internet
Author: User

serialization, deserialization there is another way to serialize, in addition to binary binary: SOAP Serialization.

Create a class, and also don't forget to serialize the tag [Serializable]

[Serializable] Public classstudentdata{ PublicStudentdata () {}Private string_code;  Public stringCode {Get{return_code;} Set{_code =value;} }    Private string_name;  Public stringName {Get{return_name;} Set{_name =value;} }    Private string_nation;  Public stringNation {Get{return_nation;} Set{_nation =value;} }}

To add a reference in CS:

Using system.io;//Flow

Using the System.runtime.serialization.formatters.soap;//soap class,

The SOAP class needs to add references to the project first, right--add references to the Reference manager interface, search for SOAP, check and determine:


Code for serializing and deserializing buttons (note the bold part of the text, here is the main difference from binary serialization):

//Serialization Button    protected voidButton1_Click (Objectsender, EventArgs e) {Studa SS=NewStuda {Code=TextBox1.Text, Name=TextBox2.Text, Nation=TextBox3.Text}; FileStream FS=NULL; Try        {            stringPath = Server.MapPath ("Data/bbb.txt");//Map the physical path of the hard disk on the server sideFS =NewFileStream (path, filemode.create);//Create a file stream            SoapFormatter bf = new SoapFormatter (); // Soap Formatter Bf. Serialize (FS, SS);//serialization of Objects        }        finally        {            if(FS! =NULL) {fs. Close ();//The stream must be closed after it is used.             }        }    }    //deserialization    protected voidButton2_Click (Objectsender, EventArgs e) {        stringPath = Server.MapPath ("Data/bbb.txt"); FileStream FS=NULL; Try{FS=NewFileStream (path, FileMode.Open);//open a file with a streamSoapFormatter             by= new SoapFormatter (); // Soap Converters Studa sdata= (Studa) by. Deserialize (FS);//deserialization into an objectLabel1.Text =sdata.            Code; Label2.Text=sdata.            Name; Label3.text=sdata.        Nation; }        finally        {            if(FS! =NULL) {fs.            Close (); }        }    }

Enter text in the text box:

Serialization results:

Here, you can visually see the text information stored in the variable in the object.

Deserialization:

Summary: Binary serialization is not miscible with SOAP serialization, and serialization using a binary method must be deserialized using a binary method. \ \ \ \ In addition, serialization can be put into SQL, and streams using binary serialization can be stored in an IMG-type column of SQL.

20150317--serialization, deserialization (2)

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.