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

 

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;
            }
        } 

    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值