.NET 对象的序列化与反序列化 Binary,Soap,Xml

测试序列化的Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.Serialization;
using System.Xml.Serialization;



namespace SerializationDemo
{
    [Serializable]
    public class UserInfo
    {
        public UserInfo()
        {

        }

        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        [NonSerialized]
        private string descrition;  //标记为NonSerialized的不会被序列化

        public string Descrition
        {
            get { return descrition; }
            set { descrition = value; }
        }

        /***
         * 在序列化为xml时 标记为
         * [XmlAnyAttribute]        成员将作为XML的特性被序列化
         * [XmlElementAttribute]    字段或者特性将作为XML元素被序列化
         * [XmlEnumAttribute]       枚举成员的元素名称
         * [XmlRootAttribute]       该特性控制根元素如何被构造(命名空间和元素名称)
         * [XmlTextAttribute]       属性或字段将被序列化为XML文本
         * [XmlTypeAttribute]       XML类型的名称和命名空间
         * */

        [XmlAttribute]
        private int age;    // 标记为XmlAttribute的将在序列化为xml时作为node的属性

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        private int sexy;

        public int Sexy
        {
            get { return sexy; }
            set { sexy = value; }
        }

        private bool hasBaby;

        public bool HasBaby
        {
            get { return hasBaby; }
            set { hasBaby = value; }
        }

        [OnSerializing]
        private void OnSerializing(StreamingContext context)
        {
            //在序列化之时被调用的事件
        }

        [OnDeserialized]
        private void OnDeserialized(StreamingContext context)
        {
            //在序列化完成后立即被调用的事件
        }

        [OnDeserializing]
        private void OnDeserializing(StreamingContext context)
        {
            //在反序列化时被调用的方法
        }

        [OnDeserialized]
        private void OnDeserialized(StreamingContext context)
        {
            //在反序列化后被立即调用的方法
        }
    }
}


序列化


private UserInfo m_User;

            m_User = new UserInfo();
            m_User.UserName = "张三";
            m_User.Descrition = "测试序列化";
            m_User.Sexy = 0;
            m_User.Age = 15;
            m_User.HasBaby = false;
        


        private void SerializationObjectGraph(IFormatter itfFormat, Stream destStream, object graph)
        {
            itfFormat.Serialize(destStream, graph);
        }

        private void btnBinaryFormatter_Click(object sender, EventArgs e)
        {
            try
            {
                BinaryFormatter binFormat = new BinaryFormatter();
                using (Stream fStream = new FileStream("User.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    SerializationObjectGraph(binFormat, fStream, m_User);
                    MessageBox.Show("Success!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSoapFormater_Click(object sender, EventArgs e)
        {
            try
            {
                SoapFormatter soapFormat = new SoapFormatter();
                using (Stream fStream = new FileStream("User.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    SerializationObjectGraph(soapFormat, fStream, m_User);
                    MessageBox.Show("Success!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void btnXmlFormater_Click(object sender, EventArgs e)
        {
            try
            {
                XmlSerializer xmlFormat = new XmlSerializer(typeof(UserInfo));
                using (Stream fStream = new FileStream("User.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    xmlFormat.Serialize(fStream, m_User);
                    MessageBox.Show("Success!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值