天天看点

c#序列化、反序列化实例

序列化就是将一个对象的状态持久化,把当前对象的信息保存到文件中,在需要时反序列化获取该对象的信息。

下面是序列化与反序列化的实例。

c#序列化、反序列化实例

输入如图所示的信息,点击输出格式内的二进制格式按钮,将输入当前对象信息的二进制格式文件(默认保存在当前工程的bin目录下),在进行反序列化时,在反序列化框的TextBox内输入binar.dat也就是已经输出的文件,点击二进制格式的反序列化按钮即可获得以上序列化后保存的对象的信息。

其他两种格式的也类似。至于这三种序列化方式的区别及优缺点在下一篇日志中会用实例来描述的,现在先来了解如何将对象序列化吧。

下面是上图对应得后台代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

using System.Xml.Serialization;

using System.Runtime.Serialization.Formatters.Soap;

namespace serialize

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void btnBinary_Click(object sender, EventArgs e)

        {

            //创建IFormatter接口引用,来自于BinaryFormatter类对象

            IFormatter Fmt = new BinaryFormatter();

            //定义成功信息

            string Success = "二进制输出成功";

            //调用UseIFormatter方法,并传递Fmt和Success参数

            UseIFormatter(Fmt, Success);

        }

        private void UseIFormatter(IFormatter Fmt, string Success)

        {

            try

            {

                PersonName personName = new PersonName(this.txtName.Text, this.txtNicheng.Text, this.txtPassword.Text);

                using (Stream fs = new FileStream(this.txtFileName.Text, FileMode.Create, FileAccess.Write, FileShare.None))

                {

                    Fmt.Serialize(fs, personName);

                }

                MessageBox.Show(Success);

            }

            catch

            {

            }

        }

        [Serializable]

        public class PersonName

        {

        [NonSerialized]

            public string name;

            [NonSerialized]

            public string nickName;

            public PersonOther po = new PersonOther();

            public PersonName() { }

            public PersonName(string name,string nickName,string pwd)

            {

                this.name = name;

                this.nickName = nickName;

                this.po.Password = pwd;

            }

        }

        [Serializable]

        public class PersonOther

        {

            string pwd;

            public PersonOther() { }

            public string Password

            {

                get

                {

                    return pwd;

                }

                set

                {

                    pwd = value;

                }

            }

        }

        private void btnXML_Click(object sender, EventArgs e)

        {

            PersonName pn = new PersonName(this.txtName.Text, this.txtNicheng.Text, this.txtPassword.Text);

            XmlSerializer Xs = new XmlSerializer(typeof(PersonName), new Type[] { typeof(PersonOther) });

            //创建Stream类型引用fs,并传递fn作路径参数

            using (Stream fs = new FileStream(this.txtFileName.Text, FileMode.Create, FileAccess.Write, FileShare.None))

            {

                Xs.Serialize(fs, pn);

            }

            MessageBox.Show("xml格式输出成功。");

        }

        private void btnSoap_Click(object sender, EventArgs e)

        {

            //创建IFormatter接口引用,来自于SoapFormatter类对象

            IFormatter Fmt = new SoapFormatter();

            //定义成功信息

            string Success = "Soap输出成功";

            //调用UseIFormatter方法,并传递Fmt和Success参数

            UseIFormatter(Fmt, Success);

        }

        private void btnDesBinary_Click(object sender, EventArgs e)//二进制反序列化的按钮事件

        {

            using(FileStream fs=new FileStream(textBox1.Text,FileMode.Open))

            {

                IFormatter fmt = new BinaryFormatter();

                PersonName person=(PersonName)fmt.Deserialize(fs);

                txtName.Text = person.name;

                txtPassword.Text = person.po.Password;

                txtNicheng.Text = person.nickName;

            }

        }

        private void btnDesSoap_Click(object sender, EventArgs e)//Soap的反序列化按钮事件

        {

            using (FileStream fs = new FileStream(textBox1.Text.Trim(), FileMode.Open))

            {

                IFormatter fmt = new SoapFormatter();

                PersonName person = (PersonName)fmt.Deserialize(fs);

                txtName.Text = person.name;

                txtNicheng.Text = person.nickName;

                txtPassword.Text = person.po.Password;

            }

        }

        private void btnDesXml_Click(object sender, EventArgs e)

        {

            XmlSerializer desXml = new XmlSerializer(typeof(PersonName), new Type[] { typeof(PersonOther) });

            using (Stream sm = new FileStream(this.textBox1.Text.Trim(), FileMode.Open))

            {

                PersonName person=(PersonName) desXml.Deserialize(sm);

                this.txtName.Text = person.name;

                txtNicheng.Text = person.nickName;

                txtPassword.Text= person.po.Password;

            }

        }

    }

}

http://hi.baidu.com/zhaochenbo/blog/item/bfd044a7d300fb8ed0435826.html