zoukankan      html  css  js  c++  java
  • web 序列化

    序列化:将对象转换为二进制 object->二进制 可以写到硬盘文件也可以存到内存文件文件里 。流


    反序列化:将二进制数据转换为对象

    断点:窗口-即时窗口找文件存的东西

    先添加命名空间:
        using System.Runtime.Serialization.Formatters.Binary;序列化。格式化。二进制
        using System.IO;流

    //指示一个类可以序列化,但是无法继承此类

    [Serializable]
    public class MES
    {

            public string Code;
            public string Name;
            public string Mainna()
            {
                return Name + Code + "wored";
            }
        
    }
     

    //序列化
        protected void Button1_Click(object sender, EventArgs e)
        {


            //一:造一个对象
            MES data = new MES();
            data.Code = "008";
            data.Name = "壮族";

            //二:将对象写到文件中
            
    //1.找到文件路径,并改为绝对路径,准备一个文件流
            string path = Server.MapPath("wenjian/aa.txt");
            //文件有就打开。没有就创建
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate);

            //2.序列化对象
            BinaryFormatter bf = new BinaryFormatter();
            //流,加一个对象
            bf.Serialize(fs, data);

            //流要最后关闭
            fs.Close();


        }
        //反序列化
        protected void Button2_Click(object sender, EventArgs e)
        {
            //找到文件路径,并转化为绝对路径
            string path = Server.MapPath("wenjian/aa.txt");
            //造一个文件流
            FileStream fs = new FileStream(path,FileMode.Open);
       
             //饭序列化
            BinaryFormatter bf = new BinaryFormatter();
           MES data= bf.Deserialize(fs)as MES ;
           Label1.Text = data.Mainna();

        }

    班级座次表

    public partial class banji : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //序列化存到数据库用内存流
        protected void Button1_Click(object sender, EventArgs e)
        {
            //造一个数组,存内存流中的数据

            byte[] sj = null;
            //
            
    //造嘻哈表集合  HashSet<string> 一个类型的函数

            Dictionary<stringstring> list = new Dictionary<stringstring>();


            //  遍历在这个页面中的控件,用哈希表集合 key和value 值正好对应
            foreach (Control ctr in this.Page.Controls)
            {
                //is 这个对象是不是属于这个类
                if (ctr is TextBox)
                {
                    TextBox txt = ctr as TextBox;
                    list.Add(txt.ID, txt.Text);

                }
            }

            //造一个流 不早硬盘上些东西就用内存流,若是想硬盘上存东西用文件流 
            MemoryStream ms = new MemoryStream();

            //序列化
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, list);

            //将内存流中的数据传到数组
            ms.Seek(0, SeekOrigin.Begin);//偏移量,从哪个位置开始便宜

            sj = ms.ToArray();

            XuLie data = new XuLie();
            data.Name = "班级信息";
            data.Content = sj;//Content的数据类型为image

            
    //存到数据库
            TestDataContext context = new TestDataContext();
            context.XuLie.InsertOnSubmit(data);
            context.SubmitChanges();

            ////造一个数组
            //byte[] sj = null;

            ////造哈希表集合  
            //Dictionary<string, string> list = new Dictionary<string, string>();
            ////用哈希表集合存储内容key 和value 是对应的
            //foreach (Control ctl in this.form1.Controls)
            
    //{
            
    //    if (ctl is TextBox)
            
    //    {
            
    //        TextBox txt = ctl as TextBox;
            
    //        list.Add(txt.ID, txt.Text);
            
    //    }
            
    //}
            ////造一个内存流
            //MemoryStream ms = new MemoryStream();
            ////序列化
            //BinaryFormatter bf = new BinaryFormatter();
            
    //bf.Serialize(ms, list);

            ////将内存流中的数据传给数组 

            //sj = ms.ToArray();

            
    //XuLie data = new XuLie();
            
    //data.Name = "班级信息";
            
    //data.Content = sj;

            ////存到数据库
            //TestDataContext context = new TestDataContext();
            
    //context.XuLie.InsertOnSubmit(data);
            
    //context.SubmitChanges();

            
    //ms.Close();


        }
        //饭序列化取出数据
        protected void Button2_Click(object sender, EventArgs e)
        {
            //找出数据
            TestDataContext context = new TestDataContext();
            XuLie data = context.XuLie.Where(p => p.Ids == 2).First();
            byte[] sj = data.Content.ToArray();

            //
            MemoryStream ms = new MemoryStream();
            if (sj != null)
            {
                ms.Write(sj, 0, sj.Length);
            }
            ms.Seek(0, SeekOrigin.Begin);
            //反序列化
            BinaryFormatter bf = new BinaryFormatter();
            Dictionary<stringstring> list = bf.Deserialize(ms) as Dictionary<stringstring>;

            //显示找具体的form表单或者是加一个 panel 找panel的模板

            foreach (Control ctl in this.form1.Controls)
            {
                if (ctl is TextBox)
                {
                    TextBox txt = ctl as TextBox;

                    txt.Text = list[txt.ID].ToString();

                }
            }
            ms.Close();
  • 相关阅读:
    数据预处理--数据清洗
    数据运营(一)基本讲解概念及运营方式.
    基于skearn-learn 框架 的线性回归
    手写线性回归算法demo
    数据分析算法--线性回归算法讲解(2)
    数据分析算法--线性回归算法讲解
    数据监督与非监督
    numpy的常用函数以及常规操作
    数据分析--基础numpy(一)
    互斥锁,自旋锁,读写锁与原子操作
  • 原文地址:https://www.cnblogs.com/cf924823/p/5157612.html
Copyright © 2011-2022 走看看