C # serialization and deserialization Overview

Source: Internet
Author: User

Before exploring C # serialization and deserialization, we must first understand what serialization is, also known as serialization. It is a mechanism used by the. NET runtime environment to support user-defined streaming. Serialization is to save an object to a file or database field. deserialization is to convert the file to the original object when appropriate. The objective is to make the custom object persistent by storage, or to transmit the object from one place to another .. NET Framework provides two serializable methods: 1. Using BinaryFormatter for serialization; 2. Using SoapFormatter for serialization; 3. Using XmlSerializer for serialization. The first method provides a simple binary data stream and some additional types of information, while the second method formats the data stream for XML storage. The third method is similar to the second method in XML format storage, it is much simpler than the second XML format (removes the extra information unique to SOAP ). You can use the [Serializable] attribute to serialize a class. If the elements of a class do not want to be serialized, 1 and 2 can use the [NonSerialized] attribute for flag. 2. You can use the [XmlIgnore] flag.

Next let's get to know more about C # serialization and deserialization:

C # serialization and deserialization 1. Use BinaryFormatter for serialization

The following is a serializable class:

 
 
  1. UsingSystem;
  2. UsingSystem. Data;
  3. UsingSystem. Configuration;
  4. UsingSystem. Web;
  5. UsingSystem. Web. Security;
  6. UsingSystem. Web. UI;
  7. UsingSystem. Web. UI. WebControls;
  8. UsingSystem. Web. UI. WebControls. WebParts;
  9. UsingSystem. Web. UI. HtmlControls;
  10. UsingSystem. IO;
  11. UsingSystem. Runtime. Serialization. Formatters. Binary;
  12. /** // <Summary>
  13. /// Summary of ClassToSerialize
  14. /// </Summary>
  15. [Serializable]
  16. Public ClassClassToSerialize
  17. {
  18. Public IntId = 100;
  19. Public StringName = "Name ";
  20. [NonSerialized]
  21. Public StringSex = "male ";
  22. }

The following describes the serialization and deserialization methods:

 
 
  1. public void SerializeNow()  
  2. {  
  3. ClassToSerialize c = new ClassToSerialize();  
  4. FileStream fileStream =   
  5. new FileStream("c:\temp.dat", FileMode.Create);  
  6. BinaryFormatter b = new BinaryFormatter();  
  7. b.Serialize(fileStream, c);  
  8. fileStream.Close();  
  9. }  
  10. public void DeSerializeNow()  
  11. {  
  12. ClassToSerialize c = new ClassToSerialize();  
  13. c.Sex = "kkkk";  
  14. FileStream fileStream =  
  15.  new FileStream("c:\temp.dat",   
  16. FileMode.Open, FileAccess.Read, FileShare.Read);  
  17. BinaryFormatter b = new BinaryFormatter();  
  18. c = b.Deserialize(fileStream) as ClassToSerialize;  
  19.   Response.Write(c.name);  
  20. Response.Write(c.Sex);  
  21. fileStream.Close();  

Call the above two methods to see the serialized result: the Sex attribute is always null because it is marked as [NonSerialized.

C # serialization and deserialization 2. Use SoapFormatter for serialization

Similar to BinaryFormatter, you only need to make a simple modification:

A. Change. Formatter. Binary in the using statement to. Formatter. Soap;

B. replace all BinaryFormatter with SoapFormatter.

C. Make sure that the file name is. xml.

After the above simple changes, SoapFormatter can be serialized. The generated file is an xml file.

C # serialization and deserialization 3. Use XmlSerializer for serialization

There is another question about the formatter. Suppose we need XML, but we do not want additional information unique to SOAP. What should we do? There are two solutions: either compile a class that implements the IFormatter interface, using a method similar to the SoapFormatter class, but there is no information you don't need; or 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 System. Xml. Seriali.

Related Article

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.