例程:二进制序列化和Soap序列化
来源:互联网 发布:saas软件代理协议 编辑:程序博客网 时间:2024/05/22 07:46
今天我们上了两种序列化。。这里便是我写的一个小例程,有兴趣的可以看看。
最后一个命名空间是手动添加的,直接写,显示不出来,添加步骤如下:
1、打开【解决方案资源管理器】
2、右击【引用】,选择【添加引用】
3、在打开的窗口中选择【.NET】选项卡,然后找到组件名称为【System.Runtime.Serialization.Formatters.Soap】的那一项
4、选中后单击【确定】按钮,然后就可以导入该命名空间了
下面是程序运行界面:
下面是部分控件的命名:
下面是源码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Runtime.Serialization.Formatters.Soap;
- namespace FJT1
- {
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- }
- List<Student> students;
- /// <summary>
- /// 将数据保存为二进制
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnTwo_Click(object sender, EventArgs e)
- {
- try
- {
- Student student = new Student(txtNum.Text, txtName.Text, Convert.ToInt32(txtAge.Text), cboSex.Text, txtHuji.Text, txtAddress.Text);
- students = new List<Student>();
- students.Add(student);
- FileStream fs = new FileStream("students.Binary", FileMode.Create);
- BinaryFormatter bf = new BinaryFormatter();
- bf.Serialize(fs, students);
- MessageBox.Show("保存成功!");
- //清空界面上的数据
- btnClear_Click(null, null);
- //关闭文件流
- fs.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString());
- }
- }
- /// <summary>
- /// 窗体加载的时候创建数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MainForm_Load(object sender, EventArgs e)
- {
- //Student student = new Student("S2T46001", "萧逸竹", 18, "男", "安徽省", "池州市");
- //students = new List<Student>();
- //students.Add(student);
- }
- /// <summary>
- /// 反汇编
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnTurnTwo_Click(object sender, EventArgs e)
- {
- FileStream fs = new FileStream("students.Binary", FileMode.Open);
- BinaryFormatter bf = new BinaryFormatter();
- students = (List<Student>)bf.Deserialize(fs);
- //将数据显示在界面上
- txtNum.Text = students[0].Number.ToString();
- txtName.Text = students[0].Name.ToString();
- txtAge.Text = students[0].Age.ToString();
- cboSex.SelectedIndex = students[0].Sex.ToString() == "男" ? 0 : 1;
- txtHuji.Text = students[0].HuJi.ToString();
- txtAddress.Text = students[0].Address.ToString();
- //关闭文件流
- fs.Close();
- //下面是显示数据
- //txtNum.Text = student.Number.ToString();
- //txtName.Text = student.Name.ToString();
- //txtAge.Text = student.Age.ToString();
- //cboSex.SelectedIndex = student.Sex.ToString() == "男" ? 0 : 1;
- //txtHuji.Text = student.HuJi.ToString();
- //txtAddress.Text = student.Address.ToString();
- }
- /// <summary>
- /// 清空界面上的数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnClear_Click(object sender, EventArgs e)
- {
- txtNum.Text = "";
- txtName.Text = "";
- txtAge.Text = "";
- cboSex.SelectedIndex = -1;
- txtHuji.Text = "";
- txtAddress.Text = "";
- }
- /// <summary>
- /// Soap序列化
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnSoap_Click(object sender, EventArgs e)
- {
- try
- {
- Student student = new Student(txtNum.Text, txtName.Text, Convert.ToInt32(txtAge.Text), cboSex.Text, txtHuji.Text, txtAddress.Text);
- FileStream fs = new FileStream("students.Soap", FileMode.Create);
- SoapFormatter sf = new SoapFormatter();
- sf.Serialize(fs, student);
- MessageBox.Show("保存成功!");
- //清空界面上的数据
- btnClear_Click(null, null);
- //关闭文件流
- fs.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString());
- }
- }
- /// <summary>
- /// Soap反序列化
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnTurnSoap_Click(object sender, EventArgs e)
- {
- FileStream fs = new FileStream("students.Soap", FileMode.Open);
- SoapFormatter sf = new SoapFormatter();
- Student student = (Student)sf.Deserialize(fs);
- //将数据显示在界面上
- txtNum.Text = student.Number.ToString();
- txtName.Text = student.Name.ToString();
- txtAge.Text = student.Age.ToString();
- cboSex.SelectedIndex = student.Sex.ToString() == "男" ? 0 : 1;
- txtHuji.Text = student.HuJi.ToString();
- txtAddress.Text = student.Address.ToString();
- //关闭文件流
- fs.Close();
- }
- }
- }
- 例程:二进制序列化和Soap序列化
- 文本文件序列化和二进制序列化
- .NET提供的二进制序列化和xml序列化
- Boost文本序列化和二进制序列化的效率
- 二进制序列化 BinaryFormater
- 二进制序列化
- Serialization之SOAP序列化
- C#-二进制和XML序列化
- 压缩解压和二进制序列化DEMO
- boost 文本和二进制序列化
- 对象序列化(二进制序列化)
- 对象的序列化与反序列化,以xml,二进制流的方式,soap方式序列化对象
- apache soap 序列化和反序列化
- .net自带二进制序列化,XML序列化和ProtoBuf序列化的压缩对比
- .Net1.0和2.0下 soap序列化和binary序列化对比
- .Net1.0和2.0下 soap序列化和binary序列化对比
- Mcad学习笔记之序列化(2进制和Soap序列化)
- android 二进制传输 序列化
- 隐藏已知文件类型的扩展名选项不可用
- 介绍一个打字练习软件——计算机达人成长之路(29)
- 我的大学
- 打地鼠
- 今天加入CSDN
- 例程:二进制序列化和Soap序列化
- web技术
- 今天真是忙得不亦乐乎!
- Linux知识99问
- Linux操作系统中七件超厉害的武器
- Linux 文件系统精通指南
- Linux 基本概念及常用命令
- 多多指教
- 第一天