asp.net serialization and deserialization demo _ Practical Tips
Source: Internet
Author: User
What is serialization?
---. Net's run-time environment is used to support the mechanism of user-defined type fluidization. It is the process of storing the state of an object instance to storage media. In this procedure, the public and private fields of the object, as well as the name of the class, including the assembly in which the class resides, are converted to a byte stream, which is then written to the data flow. When the object is deserialized later, the exact same copy of the original object is created.
Purpose of Serialization:
1, in some form of storage to make the custom object persistent;
2. Transfer objects from one place to another.
Essentially, the serialization mechanism converts the value of a class to a generic (that is, continuous) byte stream, which can then be written to a disk file or to any other streaming target. To actually write this flow, you 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;
}
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