日期:2014-05-17  浏览次数:21074 次

使用C#对对象进行序列化操作

?

/**
 * 
 * 该测试程序使用了C#的序列化进行了整个对象的序列化操作
 * 学生记录类StudentRecord 使用了list泛型类进行学生对象的存储,然后把该类
 * 进行序列化。每次需要查找数据时,又对文件中的数据进行反序列化,再进行相关操作
 */

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


namespace studentRecord
{
    class Program
    {
        static void Main(string[] args)
        {

            Student s1 = new Student();
            s1.id = "20081120029";
            s1.name = "哈桑";
            s1.major = "文学";
            s1.phone = "12345678";
            s1.sex = "男";
            s1.email = "zhyq0826@gmail.com";

            Student s2 = new Student();
            s2.id = "20081120030";
            s2.name = "阿米尔";
            s2.major = "文学";
            s2.phone = "1234567889";
            s2.sex = "男";
            s2.email = "zhyq0826@gmail.com";

            string path = "record.dat";

            StudentRecord sr = new StudentRecord();


            Console.WriteLine("开始添加学生\n{0}", s1.ToString());
            Console.WriteLine("开始添加学生\n{0}", s2.ToString());
            sr.Add(s1, path); //写入学生信息
            sr.Add(s2, path);
            Console.WriteLine("学生添加完毕");

            Console.WriteLine("-------------------------------");

            Console.WriteLine("开始查找学号为20081120029的学生");
            Student s = sr.Search("20081120029", path);
            Console.WriteLine("查找到的该生信息为\n{0}", s.ToString());


            Console.WriteLine("-------------------------------");


            Console.WriteLine("开始更新20081120029的学生信息");
            s.major = "数学";
            s.email = "zhyq0826@qq.com";
            sr.Update(s, path); //更新完毕

            s = sr.Search("20081120029", path);
            Console.WriteLine("更新完毕后20081120029的学生信息为\n{0}", s.ToString());

            Console.WriteLine("-------------------------------");


            Console.WriteLine("删除学生20081120029的信息\n");
            sr.Delete("20081120029", path);
            List<Student> list = sr.GetAll(path);
            Console.WriteLine("删除后的数据为\n");
            foreach (Student sn in list)
            {
                Console.WriteLine("{0}", sn.ToString());
            }

            if (File.Exists(path))  //程序退出之前删除学生信息值,方便测试程序
            {
                File.Delete(path);
            }

            Console.ReadKey();
        }
    }

    [Serializable]  //标志文件可以进行序列化
    class Student {

        public string email { set; get; }
        public string name { set; get; }
        public string id { set; get; }
        public string sex { set; get; }
        public string birth { set; get; }
        public string phone { set; get; }
        public string major { set; get; }

        public override string ToString()
        {
            return "学号:" + this.id + ",\n" + "名字:" + this.name + ",\n" + "性别:" + this.sex + ",\n" + "出生年月:" + this.birth + ",\n" + "专业:" + this.major + ",\n" + "联系电话:" + this.phone + ",\n" + "email:" + this.email + ",\n";
        }

    }

    class StudentRecord
    {
        private IFormatter formatter;
        private Stream stream;
        private List<Student> list;

        public StudentRecord()
        {
            formatter = new BinaryFormatter();
        }

        public List<Student> GetAll(string path)
        {
            OpenFileForRead(path);
            return list;
        }

        public void Add(Student st,string path)
        {
            OpenFileForRead(path);
            if (list == null)
            {
                list = new List<Student>();
                list.Add(st);
                OpenFileForWrite(path);  //打开文件进行写入
                return;
            }else {
                list.Add(st);
                OpenFileForWrite(path);  //打开文件进行写入
                return;
            }
        }

        public void Update(Student st,string path)