Difference Between XmlSerialization and BinarySerialization

Serialization is a process that permits to convert the state of an object into a form that can be stored into some durable medium (file, DB) to rebuild original object in the future or to send the object to another process or computer through network channels.

1. Xml Serialization in .net, has probably a wrong name, because it does not really serialize an object, it only create an XML stream with the content of all the public properties, it does not store private fields, and it is not meant to create a stream to save object state into some durable medium. Xml Serialization is meant as a technique to read and write XML stream to and from an object model. If you have a schema file, you can use xsd.exe tool to generate a set of classes that can be used to create in memory an object representation of a xml file that satisfies that schema.

2. XmlSerializer can serialize a class that is not marked as serializable. This is possible because XmlSerializer does not really serialize the object it simply create a xml stream. If you have a class that simply dumps whenever the default constructor is called.

3. when you deserialize an object with XmlSerializer you can see that this constructor gets called. If you deserialize an object with a BinaryFormatter the constructor is not called. This is perfectly reasonable, since serialization is meant to convert an object to a binary stream and back, so you cannot call the default constructor during deserialization because you can end in having a different object from the original one. 

4. Xml Serialization does not check for object identity , thus it throws exception if the object graph has a circular references. but Binary serialization can correctly handles object graphs and circular references automatically.

ok . let 's have some test to prove the above list points.

[Serializable]// xmlserialize do not need this attribute this is for binaryserialize
public class Testcontainer
{
    public TestClass test1 { get; set; }
    public TestClass test2 { get; set; }
}

[Serializable] // xmlserialize do not need this attribute this is for binaryserialize
    public class TestClass
    {
        public TestClass()
        {
            Console.WriteLine("Test Class Constructor is called.");
        }
        public string name;
    }

here is the test class defined.

            TestClass test = new TestClass();
            test.name = "wq";
            MemoryStream xms = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(typeof(TestClass));
            xs.Serialize(xms, test);
            Console.WriteLine("Xml Serialized.");

            xms.Position = 0;
            TestClass des = (TestClass)xs.Deserialize(xms);
            Console.WriteLine("TestClass.Name = " + des.name);
            Console.ReadLine();

above code sample prove point 3. there should be shown a message writen in the TestClass when Deserializing TestClass. the result in Binary Deserializing abhorrent to it .

            Testcontainer t = new Testcontainer();
            t.test1 = t.test2 = new TestClass() { name = "Test" };
            MemoryStream xms1 = new MemoryStream();
            XmlSerializer xs1 = new XmlSerializer(typeof(Testcontainer));
            xs1.Serialize(xms1, t);

            xms1.Position = 0;
            Testcontainer t2 = (Testcontainer)xs1.Deserialize(xms1);
            Console.WriteLine("t.test1 == t.test2 is {0}", t2.test1 == t2.test2);

            MemoryStream bms1 = new MemoryStream();
            BinaryFormatter bf1 = new BinaryFormatter();
            bf1.Serialize(bms1, t);
            bms1.Position = 0;
            Testcontainer t1 = (Testcontainer)bf1.Deserialize(bms1);
            Console.WriteLine("t.test1 == t.test2 is {0}", t1.test1 == t1.test2);
            Console.ReadLine();

the above code proved point 4. the message shown in console.

"t.test1==t.test2 is false"

"t.test1==t.test2 is true"

 

 

 

原文地址:https://www.cnblogs.com/malaikuangren/p/2576364.html