Serialization- C# 读书笔记

 

There are three major formats of serialization available: binary, XML and SOAP.

1.       Binary Serialization

1.1   Characteristic

·         Produces a non-printable sequence of byte-oriented data that represents the source object

·         It retains type information within the generated data stream for the source object

1.2 Steps to serialize and deserialize

Before an object can be binary serialized, it must be marked as such by adding the Serializable attribute to the Class as below:

    [Serializable]

    public class MyBasicData

    {

        public int IntField1;

        public string StringField;

        private int IntField2;

        public MyBasicData()

        {

            IntField1 = 100;

            StringField = "hello";

            IntField2 = 200;

        }

    }

Then create an instance of the formatter which will perform the work of serializing the class. The binary formatter implements the IFormatter interface; here is the example that can serialize an object to a file by file stream:

            MyBasicData myData = new MyBasicData();

            FileStream binFileStream = new FileStream("e:\\Bin_ser.bin", FileMode.Create, FileAccess.Write, FileShare.None);

 

            IFormatter binFormatter = new BinaryFormatter();

 

            try

            {

                binFormatter.Serialize(binFileStream, myData);

 

                binFileStream.Close();

 

            }

            catch (SerializationException err)

            {

                Console.WriteLine(err.Message);

            }

We can also use the serialized file to retrieve the object data:

binFileStream = new FileStream("e:\\Bin_ser.bin", FileMode.Open, FileAccess.ReadWrite, FileShare.None);

            MyBasicData res = (MyBasicData)binFormatter.Deserialize(binFileStream);

            Console.WriteLine(res.IntField1);

1.3 Controlling Binary Serialization

If you don’t want all the class data serialized( such as Password data), there are two ways to realize that. First, using NonSerialized attribute to the member field, the other one is to implement customized ISerializable interface

·         Add NonSerialized attribute

[Serializable]

    public class MyBasicData

    {

        public int IntField1;

        [NonSerialized]

        public string StringField;

        private int IntField2;

        public MyBasicData()

        {

            IntField1 = 100;

            StringField = "hello";

            IntField2 = 200;

        }

}

·         Implement Customized ISerializable interface

It must implement the following two methods:

void GetObjectData(SerializationInfo info, StreamingContext context)

protected MyObjectConstructor(SerializationInfo info, StreamingContext context)

Detailed info please search from MSDN

2.       XML Serialization

When use XML Serialization, it does not need to add Serializable attribute to the serialized class. And it’s very simple to serialize and deserialize objects

2.1 Serialization:

FileStream fileStream = new FileStream("e:\\xml.xml", FileMode.Create, FileAccess.Write, FileShare.None);

XmlSerializer xml = new XmlSerializer(typeof(MyBasicData));

xml.Serialize(fileStream, myData);

2.2 Deserialization:

FileStream fileStream = new FileStream("e:\\xml.xml", FileMode.Open, FileAccess.Write, FileShare.None);

XmlSerializer xml = new XmlSerializer(typeof(MyBasicData));

myData=xml.Serialize(fileStream);

Here we can also use XML attributes to format produced xml file, for example

public class MyBasicData

    {

        [XmlElement(ElementName = "Inter_Field")]

        public int IntField1;   

}

It will use Inter_Field to replace of IntField1 attribute in produced xml serialized file

3.       SOAP Serialization

It has the same way as binary serialization, just use SoapFormatter to replace of BinaryFormatter, please see the above introduction to Binary Serialization

4.       Conclusion

At last, I quote one part of .NET FRAMEWORK NETWORK PROGRAMMING as our conclusion:

The .NET Framework offers three types of serialization, and each has its advantages. Binary serialization is the easiest of all serializes to use and also produces the most compact data. The XML serializer offers interoperability and portability at the expense of a much larger serialized data size. Finally, serializing to SOAP allows interoperability with SOAP-based services such as .NET Remoting and Web services. It’ evident that serialization is a powerful and very useful mechanism for transporting complex data across processes, regardless of whether they’re running on the same machine or across a network.

转载于:https://www.cnblogs.com/Jiansong/archive/2010/03/22/1691735.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值