.net framework中支持二进制序列化、SOAP序列化、Xml序列化和自定义的序列化。
如果有一个学生类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Xml.Serialization;//XML序列化 namespace StudentManage { [Serializable] //指定可以序列化 public class Student:IComparable<Student> { public string ID; public string Name; public float ChineseScore; public float MathScore; public float EnglishScore; public float SumScore { get { return ChineseScore + MathScore + EnglishScore; } } public float AverageScore { get { return SumScore / 3; } } public Student() { this.ID = ""; this.Name = ""; this.ChineseScore = 0; this.MathScore = 0; this.EnglishScore = 0; } int IComparable<Student>.CompareTo(Student other) { return this.SumScore.CompareTo(other.SumScore); } } }
还有一个学生列表类,其中包括一个学生类的集合:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Windows.Forms; using System.Xml.Serialization; namespace StudentManage { [Serializable] public class StudentList { //学生列表 [XmlElement] private List<Student> StuList = new List<Student>(); public List<Student> stuList { get { return StuList; } set { StuList = value; } } //索引器 public Student this[int index] { get { if (index < 0 || index > StuList.Count) { return null; } return StuList[index]; } set { if (index < 0 || index >= StuList.Count) { return; } StuList[index] = value; } } //学生人数 public int StuCount { get { return StuList.Count; } } //添加一个学生 public bool AddStudent(Student stu) { Student s = StuList.Find(e => e.ID == stu.ID); if (s == null) { StuList.Add(stu); return true; } else { return false; } } public bool DeleteStudent(string stuID) { Student s = StuList.Find(e => e.ID == stuID); { if (s == null) { return false; } else { StuList.Remove(s); return true; } } } public Student QueryStudent(string stuID) { Student s = StuList.Find(e => e.ID == stuID); { return s; } } public int[][] GetScorePercentArray() { int[][] a = new int[5][]; for (int i = 0; i < 5; i++) a[i] = new int[3]; for (int i = 0; i < StuList.Count; i++) { //语文成绩统计 if (StuList[i].ChineseScore >= 90) a[0][0] += 1; else if (StuList[i].ChineseScore >= 80) a[1][0] += 1; else if (StuList[i].ChineseScore >= 70) a[2][0] += 1; else if (StuList[i].ChineseScore >= 60) a[3][0] += 1; else a[4][0] += 1; //数学成绩统计 if (StuList[i].MathScore >= 90) a[0][1] += 1; else if (StuList[i].MathScore >= 80) a[1][1] += 1; else if (StuList[i].MathScore >= 70) a[2][1] += 1; else if (StuList[i].MathScore >= 60) a[3][1] += 1; else a[4][1] += 1; //英语成绩统计 if (StuList[i].EnglishScore >= 90) a[0][2] += 1; else if (StuList[i].EnglishScore >= 80) a[1][2] += 1; else if (StuList[i].EnglishScore >= 70) a[2][2] += 1; else if (StuList[i].EnglishScore >= 60) a[3][2] += 1; else a[4][2] += 1; } return a; } public void Sort() { StuList.Sort(); StuList.Reverse(); } public Student GetChineseMaxStudent() { float max = StuList[0].ChineseScore; int maxIndex = 0; for (int i = 1; i < StuList.Count; i++) { if (StuList[i].ChineseScore > max) { max = StuList[i].ChineseScore; maxIndex = i; } } return StuList[maxIndex]; } } }
我要序列化学生列表。
namespace StudentManage { public partial class StudentManage : Form { private Point startPosition;//单击鼠标时鼠标的位置 //学生列表 private static StudentList stuList = new StudentList();
首先定义2个用于序列化和反序列化的方法:
private static void SerializeStudent(StudentList stuList, FileStream fs) { //BinaryFormatter formatter = new BinaryFormatter(); XmlSerializer formatter = new XmlSerializer(typeof(StudentList)); try { formatter.Serialize(fs, stuList); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private static StudentList DeserializeStudent(FileStream fs) { //BinaryFormatter formatter = new BinaryFormatter(); XmlSerializer formatter = new XmlSerializer(typeof(StudentList)); try { stuList = (StudentList)formatter.Deserialize(fs); } catch (Exception ex) { MessageBox.Show(ex.Message); } return stuList; }
在窗体的Load事件中,把文件的内容反序列化出来付给对象。
private void StudentManage_Load(object sender, EventArgs e) { if (!File.Exists("StudentList.txt")) { return; } FileStream fs = new FileStream("StudentList.txt", FileMode.Open); stuList = DeserializeStudent(fs); fs.Close(); if (stuList.stuList != null) { foreach (Student s in stuList.stuList) { StudataGridView.Rows.Add(s.ID, s.Name, s.ChineseScore, s.MathScore, s.EnglishScore, s.SumScore, s.AverageScore); } } this.RefreshChart(); }
在学生列表添加了一个学生的时候,把学生列表序列化到文件中。
if (stuList.AddStudent(s) == true) { using(FileStream fs = new FileStream("StudentList.txt",FileMode.Create)) { SerializeStudent(stuList,fs); fs.Close(); } StudataGridView.Rows.Add(s.ID, s.Name, s.ChineseScore, s.MathScore, s.EnglishScore,s.SumScore,s.AverageScore); RefreshChart(); }