首页 新闻 会员 周边

大家好,我新手主要帮我看看我写的这段代码规正不?有问题请直接指出,谢谢

0
悬赏园豆:20 [已关闭问题]
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

 

茅山野栗子的主页 茅山野栗子 | 初学一级 | 园豆:180
提问于:2010-06-10 10:26
< >
分享
其他回答(3)
0

stream的地方,最好修改为

using(FileStream stream = new FileStream(@"D:\1.Soap", FileMode.Open, FileAccess.Read))

{

  .........

}

代码简洁,还不容易出错。

dangjian | 园豆:348 (菜鸟二级) | 2010-06-10 11:30
是不是这样的话就不用 stream.Flush();133 stream.Close();134 stream.Dispose(); 这三句了,能不能在稍微解释一下
支持(0) 反对(0) 茅山野栗子 | 园豆:180 (初学一级) | 2010-06-11 08:52
@27080501:using块的作用是:结束时调用对应的dispose()方法。对于你的例子,相对于stream.Close();stream.Dispose(); 这两条语句。其实你的代码没有必要加stream.Close();这句,stream.Dispose(); 这句就够了。
支持(0) 反对(0) dangjian | 园豆:348 (菜鸟二级) | 2010-06-17 12:17
0

上面确实可以简洁。

Astar | 园豆:40805 (高人七级) | 2010-06-10 11:53
0

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();

laughter | 园豆:142 (初学一级) | 2010-06-10 15:52
哦,是renturn的有些早了,应该放到最后,谢谢
支持(0) 反对(0) 茅山野栗子 | 园豆:180 (初学一级) | 2010-06-11 08:50
0

                 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;
   }
}

 

 

还有就是代码注释写详细一些 

俺不是肥熊猫 | 园豆:18 (初学一级) | 2010-06-12 11:01
路过,不错的 .NET 本身提供的机制就让我们写代码很容易
支持(0) 反对(0) 左手程序右手诗 | 园豆:82 (初学一级) | 2010-07-18 12:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册