1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.IO;
10 using System.Runtime.Serialization.Formatters;
11 using System.Runtime.Serialization.Formatters.Binary;
12 using System.Xml.Serialization;
13 using System.Runtime.Serialization.Formatters.Soap;
14
15
16 namespace SerializeDeserialize2
17 {
18 public partial class Form1 : Form
19 {
20 public Form1()
21 {
22 InitializeComponent();
23 }
24 [Serializable]
25 public class Student
26 {
27 public int Stu_Id;
28 public string Stu_Name;
29 public string Stu_Sex;
30 public int Stu_Age;
31
32 public Student()
33 {
34 }
35 public Student(int id, string name, string sex, int age)
36 {
37 Stu_Id = id;
38 Stu_Name = name;
39 Stu_Sex = sex;
40 Stu_Age = age;
41 }
42 public void Display()
43 {
44 MessageBox.Show(Stu_Id.ToString());
45 MessageBox.Show(Stu_Name);
46 MessageBox.Show(Stu_Sex);
47 MessageBox.Show(Stu_Age.ToString());
48 }
49 }
50 Student stu1 = new Student (270805,"三","男",20);
51 private void binaryToolStripMenuItem_Click(object sender, EventArgs e)
52 {
53 BinaySerializeStudent(stu1);
54 Student stu2 = BinayDeserializeStudent();
55 stu2.Display();
56 }
57 //Binay序列化
58 public static void BinaySerializeStudent(Student stu)
59 {
60 FileStream stream = new FileStream(@"D:/1.bin", FileMode.Append, FileAccess.Write);
61 BinaryFormatter formatter = new BinaryFormatter();
62 formatter.Serialize(stream, stu);
63 stream.Flush();
64 stream.Close();
65 stream.Dispose();
66 }
67 //Binay反序列化
68 public static Student BinayDeserializeStudent()
69 {
70 FileStream stream = new FileStream(@"D:\1.bin ", FileMode.Open, FileAccess.Read);
71 BinaryFormatter formatter = new BinaryFormatter();
72 Student stu = (Student)formatter.Deserialize(stream);
73 return (stu);
74 stream.Flush();
75 stream.Close();
76 stream.Dispose();
77 }
78
79 private void xmlToolStripMenuItem_Click(object sender, EventArgs e)
80 {
81 XmlSerializer(stu1);
82 Student stu2 = XmlDeserializer();
83 stu2.Display();
84 }
85
86 //XML序列化
87 public static void XmlSerializer(Student stu)
88 {
89 FileStream stream = new FileStream(@"D:\1.Xml", FileMode.Append, FileAccess.Write);
90 XmlSerializer xmlserializer = new XmlSerializer(typeof(Student));
91 xmlserializer.Serialize(stream, stu);
92 stream.Flush();
93 stream.Close();
94 stream.Dispose();
95 }
96 // Xml反序列化
97 public static Student XmlDeserializer()
98 {
99 FileStream stream = new FileStream(@"D:\1.Xml", FileMode.Open, FileAccess.Read);
100 XmlSerializer xmlserializer = new XmlSerializer(typeof(Student));
101 Student stu = (Student)xmlserializer.Deserialize(stream);
102 return (stu);
103 stream.Flush();
104 stream.Close();
105 stream.Dispose();
106
107 }
108 private void soapToolStripMenuItem_Click(object sender, EventArgs e)
109 {
110 SoapSerializer(stu1 );
111 Student stu2 = SoapDeserializer();
112 stu1.Display();
113 }
114
115 // Soap 序列化
116 public static void SoapSerializer(Student stu)
117 {
118 FileStream stream = new FileStream(@"D:\1.Soap", FileMode.Append, FileAccess.Write);
119 SoapFormatter soapformatter = new SoapFormatter();
120 soapformatter.Serialize(stream, stu);
121 stream.Flush();
122 stream.Close();
123 stream.Dispose();
124 }
125 //Soap 反序列化
126 public static Student SoapDeserializer()
127 {
128 FileStream stream = new FileStream(@"D:\1.Soap", FileMode.Open, FileAccess.Read);
129 SoapFormatter soapformatter = new SoapFormatter();
130 Student stu=(Student )soapformatter.Deserialize(stream);
131 return (stu);
132 stream.Flush();
133 stream.Close();
134 stream.Dispose();
135 }
136
137
138 }
139 }
140
用stream的地方,最好修改为
using(FileStream stream = new FileStream(@"D:\1.Soap", FileMode.Open, FileAccess.Read))
{
.........
}
代码简洁,还不容易出错。
上面确实可以简洁。
128 FileStream stream = new FileStream(@"D:\1.Soap", FileMode.Open, FileAccess.Read);
129 SoapFormatter soapformatter = new SoapFormatter();
130 Student stu=(Student )soapformatter.Deserialize(stream);
131 return (stu); //好像这样return的太早了吧
132 stream.Flush();
133 stream.Close();
134 stream.Dispose();
public int Stu_Id;
28 public string Stu_Name;
29 public string Stu_Sex;
30 public int Stu_Age;
我觉得这些字段的访问性可以弄成 private
然后利用 属性来访问
private string stu_Name; //变量名开头小写
public string Stu_Name
{
get
{
return stu_Name;
}
set
{
stu_Name=value;
}}
还有就是代码注释写详细一些