zoukankan      html  css  js  c++  java
  • 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"

     

     

     

  • 相关阅读:
    asp.net Ctrl+回车提交
    Request.Form.Keys保存的是什么?
    让iframe子窗体取父窗体地址栏参数(querystring)
    Repeater在无数据记录时显示类似GridView空模板(EmptyDataTemplate)
    jQuery与javascript对照学习(获取父子前后元素)
    internet缓存Temp中的tmp文件
    C#反射遍历一个对象属性(小技巧)
    silverlight序列化反序列化,在c#其他程序反序列化
    Silverlight序列化反序列化(json.net)
    卸载oracle
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2576364.html
Copyright © 2011-2022 走看看