今天我们上了两种序列化。。这里便是我写的一个小例程,有兴趣的可以看看。
最后一个命名空间是手动添加的,直接写,显示不出来,添加步骤如下:
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();
- }
- }
- }