Serialization [translation]

The following is an attempt to translate their own MSDN article

Original: http://msdn.microsoft.com/en-us/library/7ay27kt9(v=vs.110).aspx

Serialization is the object into a process to be transferred and stored, it is anti-deserialization process, the deserialization death refers to the process stream into the data objects. These two processes so that the storage and transmission of data becomes easy.

 

.NET Framework There are two serialization technologies:

  • Types of binary sequence can be retained, stored in an object between different applications. For example, you can use binary serialization of the object serialization methods and stored on the clipboard. You can target sequence into the data stream in the hard disk, memory, and then transmitted through the network, transmit the data to another computer, or application domain.
  • XML serialization serialize only public properties and members, and does not preserve type. XML is a public standard form. The number of columns of the XML data between the common web is a good choice. For example: SOAP.

Quote

System.Runtime.Serialization: used to serialize objects

System.Xml.Serialization: used to serialize an object file or an XML data stream

    

How to serialize objects

First you need to create an object and set its property and members of the public, then you must define the format to be transmitted, the data stream or file.

   public class test
    {
        public string name;
        public int age;
        private double money;

        public test()
        {
            name = "Cathy";
            age = 24;
            money = 10000.00;
 
        }


    }

            test Cathy = new test();
            XmlSerializer mySerializer = new XmlSerializer(typeof(test));
            StreamWriter myWriter = new StreamWriter("myFileName.xml");
            mySerializer.Serialize(myWriter, Cathy);
            myWriter.Close();

The generated XML file as follows:

  <?xml version="1.0" encoding="utf-8" ?> 
- <test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <name>Cathy</name> 
  <age>24</age> 
  </test>

 

How deserialized object

When you deserialize an object, you decide the transmission format is to build a data stream or file.

            test Cathy;
            XmlSerializer mySerializer = new XmlSerializer(typeof(test));
            FileStream myFileStream = new FileStream("myFileName.xml", FileMode.Open);
            Cathy = (test)mySerializer.Deserialize(myFileStream);

 

 

 

Reproduced in: https: //www.cnblogs.com/Jenny90/p/3613836.html

Guess you like

Origin blog.csdn.net/weixin_33842328/article/details/93566849