zoukankan      html  css  js  c++  java
  • 序列化和反序列化

    两种方法:

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

  • 相关阅读:
    一个接口,多个实现
    jmeter压力测试
    程序卡住了还是怎么了?
    Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)
    Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)
    Redis基于Java的客户端SDK收集
    Ubuntu清理内存命令(效果不明显)
    Memcached与Spring集成的方式(待实践)
    Memcached的几种Java客户端(待实践)
    Redis集群方案收集
  • 原文地址:https://www.cnblogs.com/huaze/p/4356797.html
Copyright © 2011-2022 走看看