例程:二进制序列化和Soap序列化

来源:互联网 发布:saas软件代理协议 编辑:程序博客网 时间:2024/05/22 07:46

今天我们上了两种序列化。。这里便是我写的一个小例程,有兴趣的可以看看。

最后一个命名空间是手动添加的,直接写,显示不出来,添加步骤如下:

     1、打开【解决方案资源管理器】

     2、右击【引用】,选择【添加引用】

     3、在打开的窗口中选择【.NET】选项卡,然后找到组件名称为【System.Runtime.Serialization.Formatters.Soap】的那一项

     4、选中后单击【确定】按钮,然后就可以导入该命名空间了

下面是程序运行界面:

下面是部分控件的命名:

下面是源码:

Code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.IO;  
  9. using System.Runtime.Serialization.Formatters.Binary;  
  10. using System.Runtime.Serialization.Formatters.Soap;  
  11.   
  12. namespace FJT1  
  13. {  
  14.     public partial class MainForm : Form  
  15.     {  
  16.         public MainForm()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         List<Student> students;  
  22.   
  23.         /// <summary>  
  24.         /// 将数据保存为二进制  
  25.         /// </summary>  
  26.         /// <param name="sender"></param>  
  27.         /// <param name="e"></param>  
  28.         private void btnTwo_Click(object sender, EventArgs e)  
  29.         {  
  30.             try  
  31.             {  
  32.                 Student student = new Student(txtNum.Text, txtName.Text, Convert.ToInt32(txtAge.Text), cboSex.Text, txtHuji.Text, txtAddress.Text);  
  33.                 students = new List<Student>();  
  34.                 students.Add(student);  
  35.   
  36.                 FileStream fs = new FileStream("students.Binary", FileMode.Create);  
  37.                 BinaryFormatter bf = new BinaryFormatter();  
  38.                 bf.Serialize(fs, students);  
  39.                 MessageBox.Show("保存成功!");  
  40.                 //清空界面上的数据  
  41.                 btnClear_Click(nullnull);  
  42.                 //关闭文件流  
  43.                 fs.Close();  
  44.             }  
  45.             catch (Exception ex)  
  46.             {  
  47.                 MessageBox.Show(ex.Message.ToString());  
  48.             }  
  49.         }  
  50.   
  51.         /// <summary>  
  52.         /// 窗体加载的时候创建数据  
  53.         /// </summary>  
  54.         /// <param name="sender"></param>  
  55.         /// <param name="e"></param>  
  56.         private void MainForm_Load(object sender, EventArgs e)  
  57.         {  
  58.             //Student student = new Student("S2T46001", "萧逸竹", 18, "男", "安徽省", "池州市");  
  59.             //students = new List<Student>();  
  60.             //students.Add(student);  
  61.         }  
  62.   
  63.         /// <summary>  
  64.         /// 反汇编  
  65.         /// </summary>  
  66.         /// <param name="sender"></param>  
  67.         /// <param name="e"></param>  
  68.         private void btnTurnTwo_Click(object sender, EventArgs e)  
  69.         {  
  70.             FileStream fs = new FileStream("students.Binary", FileMode.Open);  
  71.             BinaryFormatter bf = new BinaryFormatter();  
  72.   
  73.             students = (List<Student>)bf.Deserialize(fs);  
  74.   
  75.             //将数据显示在界面上  
  76.             txtNum.Text = students[0].Number.ToString();  
  77.             txtName.Text = students[0].Name.ToString();  
  78.             txtAge.Text = students[0].Age.ToString();  
  79.             cboSex.SelectedIndex = students[0].Sex.ToString() == "男" ? 0 : 1;  
  80.             txtHuji.Text = students[0].HuJi.ToString();  
  81.             txtAddress.Text = students[0].Address.ToString();  
  82.   
  83.             //关闭文件流  
  84.             fs.Close();  
  85.   
  86.             //下面是显示数据  
  87.             //txtNum.Text = student.Number.ToString();  
  88.             //txtName.Text = student.Name.ToString();  
  89.             //txtAge.Text = student.Age.ToString();  
  90.             //cboSex.SelectedIndex = student.Sex.ToString() == "男" ? 0 : 1;  
  91.             //txtHuji.Text = student.HuJi.ToString();  
  92.             //txtAddress.Text = student.Address.ToString();  
  93.         }  
  94.   
  95.         /// <summary>  
  96.         /// 清空界面上的数据  
  97.         /// </summary>  
  98.         /// <param name="sender"></param>  
  99.         /// <param name="e"></param>  
  100.         private void btnClear_Click(object sender, EventArgs e)  
  101.         {  
  102.             txtNum.Text = "";  
  103.             txtName.Text = "";  
  104.             txtAge.Text = "";  
  105.             cboSex.SelectedIndex = -1;  
  106.             txtHuji.Text = "";  
  107.             txtAddress.Text = "";  
  108.         }  
  109.   
  110.         /// <summary>  
  111.         /// Soap序列化  
  112.         /// </summary>  
  113.         /// <param name="sender"></param>  
  114.         /// <param name="e"></param>  
  115.         private void btnSoap_Click(object sender, EventArgs e)  
  116.         {  
  117.             try  
  118.             {  
  119.                 Student student = new Student(txtNum.Text, txtName.Text, Convert.ToInt32(txtAge.Text), cboSex.Text, txtHuji.Text, txtAddress.Text);  
  120.   
  121.                 FileStream fs = new FileStream("students.Soap", FileMode.Create);  
  122.                 SoapFormatter sf = new SoapFormatter();  
  123.                 sf.Serialize(fs, student);  
  124.                 MessageBox.Show("保存成功!");  
  125.                 //清空界面上的数据  
  126.                 btnClear_Click(nullnull);  
  127.                 //关闭文件流  
  128.                 fs.Close();  
  129.             }  
  130.             catch (Exception ex)  
  131.             {  
  132.                 MessageBox.Show(ex.Message.ToString());  
  133.             }  
  134.         }  
  135.   
  136.         /// <summary>  
  137.         /// Soap反序列化  
  138.         /// </summary>  
  139.         /// <param name="sender"></param>  
  140.         /// <param name="e"></param>  
  141.         private void btnTurnSoap_Click(object sender, EventArgs e)  
  142.         {  
  143.             FileStream fs = new FileStream("students.Soap", FileMode.Open);  
  144.             SoapFormatter sf = new SoapFormatter();  
  145.   
  146.             Student student = (Student)sf.Deserialize(fs);  
  147.   
  148.             //将数据显示在界面上  
  149.             txtNum.Text = student.Number.ToString();  
  150.             txtName.Text = student.Name.ToString();  
  151.             txtAge.Text = student.Age.ToString();  
  152.             cboSex.SelectedIndex = student.Sex.ToString() == "男" ? 0 : 1;  
  153.             txtHuji.Text = student.HuJi.ToString();  
  154.             txtAddress.Text = student.Address.ToString();  
  155.   
  156.             //关闭文件流  
  157.             fs.Close();  
  158.   
  159.         }  
  160.     }  
  161. }