Asp.net serialization and deserialization demonstration

Source: Internet
Author: User

What is serialization?
The runtime environment of ---.net is used to support the streaming mechanism of user-defined types. It stores the status of the object instance to the stored media. In this process, the public and private fields of the object and the name of the class (including Program Set), and then write the byte stream into the data stream. When the object is deserialized, a copy identical to the original object will be created.
Serialization objective:
1. Make the custom object persistent in a certain storage form;
2. transfer an object from one place to another.
Essentially, the serialization mechanism is to convert the class value into a general (that is, continuous) byte stream, and then the stream can be written to a disk file or any other streaming target. To write this stream, you must use the serialize and deserialize methods in the classes that implement the iformatter interface.
Private bool serializestudent (Object OBJ)
{
Filedialog FD = new savefiledialog ();
If (FD. showdialog () = dialogresult. OK | FD. filename! = NULL)
{
Filestream FS = new filestream (FD. filename, filemode. Create, fileaccess. Write );
System. xml. serialization. xmlserializer xmlser = new system. xml. serialization. xmlserializer (obj. GetType ());
Xmlser. serialize (FS, OBJ );
FS. Close ();
Return true;
}
Else
{
Return false;
}
}
Private object deserializestudent (Object OBJ)
{
Filedialog FD = new openfiledialog ();
Object objstd;
If (FD. filename! = NULL | FD. showdialog () = dialogresult. OK)
{
Filestream FS = new filestream (FD. filename, filemode. Open, fileaccess. Read, fileshare. Read );
System. xml. serialization. xmlserializer xmlser = new system. xml. serialization. xmlserializer (obj. GetType ());
Objstd = xmlser. deserialize (FS );
FS. Close ();
Return objstd;
}
Else
{
Return NULL;
}

}

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.