Difference Between XmlSerialization and BinarySerialization

來源:互聯網
上載者:User

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 binaryserializepublic 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"

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.