码迷,mamicode.com
首页 > 其他好文 > 详细

序列化和反序列化

时间:2015-03-22 10:37:02      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

两种方法:

1、using System.Runtime.Serialization.Formatters.Binary;//二进制序列化器所在命名空间
2、using System.Runtime.Serialization.Formatters.Soap;//
using System.IO;//流的

反序列化:流--->对象

一个类要想能够序列化,需要给这个类加一个attribute:即在类的定义之前加[Serializable],写在using xxxx  下面,命名空间之外。

二进制格式化器进行序列化:
 StudentData data = new StudentData
        {
             Code = TextBox1.Text,
             Name = TextBox2.Text,
             Nation = TextBox3.Text
        };//造对象
        FileStream fs = null;
        try
        {
            string path = Server.MapPath("data/aaa.txt");//映射物理路径
            fs = new FileStream(path,FileMode.Create);//建好了文件流
            BinaryFormatter bf = new BinaryFormatter();//二进制格式化器
            bf.Serialize(fs,data);//序列化方法
        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
            }
        }
反序列化:
 string path = Server.MapPath("data/aaa.txt");
        FileStream fs = null;
        try
        {
            fs = new FileStream(path,FileMode.Open);//使用流打开文件

            BinaryFormatter bf = new BinaryFormatter();//造一个二进制格式化器
            //反序列化
            StudentData sdata = (StudentData)bf.Deserialize(fs);
            TextBox1.Text = sdata.Code;
            TextBox2.Text = sdata.Name;
            TextBox3.Text = sdata.Nation;

        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
            }
        }

Soap序列化:
 StudentData data = new StudentData
        {
            Code = TextBox1.Text,
            Name = TextBox2.Text,
            Nation = TextBox3.Text
        };//造对象
        FileStream fs = null;
        try
        {
            string path = Server.MapPath("data/bbb.txt");//映射物理路径
            fs = new FileStream(path, FileMode.Create);//建好了文件流
            SoapFormatter sf = new SoapFormatter();//soap格式化器
            sf.Serialize(fs,data);//序列化
        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
            }
        }
soap反序列化:
 string path = Server.MapPath("data/bbb.txt");
        FileStream fs = null;
        try
        {
            fs = new FileStream(path, FileMode.Open);//使用流打开文件

            SoapFormatter bf = new SoapFormatter();//造一个二进制格式化器
            //反序列化
            StudentData sdata = (StudentData)bf.Deserialize(fs);
            TextBox1.Text = sdata.Code;
            TextBox2.Text = sdata.Name;
            TextBox3.Text = sdata.Nation;

        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
            }
        }

把数据序列化到数据库
  protected void Button1_Click(object sender, EventArgs e)
    {
      
        OtherData odata = new OtherData();
        odata.Hobby = TextBox4.Text;
        odata.Address = TextBox5.Text;
        odata.FatherName = TextBox6.Text;


        MemoryStream ms = new MemoryStream();      //造一个内存流
        BinaryFormatter bf = new BinaryFormatter();//二进制格式化器
        bf.Serialize(ms,odata);//将otherdata的对象序列化到内存流 ms 中去

        byte[] buffer = ms.ToArray();              //将流转换成了二进制

                                                 
        student data = new student                 //构建studentdata准备存入数据库  
        {
            xuehao = int.Parse(TextBox1.Text.Trim()),
            name = TextBox2.Text,
            sex = TextBox3.Text,
            birthday = Calendar1.SelectedDate,
            other = buffer
        };

        _Context.student.InsertOnSubmit(data);
        _Context.SubmitChanges();

    } 
从数据库把数据序列化 显示
   protected void Button2_Click(object sender, EventArgs e)
    {
        student data = _Context.student.Single(r=>r.xuehao == int.Parse(TextBox1.Text.Trim()));

        TextBox2.Text = data.name;
        TextBox3.Text = data.sex;

        byte[] buffer = data.other.ToArray();    //转化成二进制
        MemoryStream ms = new MemoryStream();    //造一个 内存流 
        ms.Write(buffer,0,buffer.Length);        //将从数据库读取到的二进制写入到内存流中,对应数据(二进制数据,起始,数量)
        ms.Seek(0,SeekOrigin.Begin);             //设置流的操作点回到开始的起点位置

        BinaryFormatter bf = new BinaryFormatter();
        OtherData odata =(OtherData)bf.Deserialize(ms); //把  内存流  转化成  OtherData 数据
        TextBox4.Text = odata.Hobby;
        TextBox5.Text = odata.Address;
        TextBox6.Text = odata.FatherName;
    }

序列化和反序列化

标签:

原文地址:http://www.cnblogs.com/huaze/p/4356797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!