我在写一个软件测试工程师管理系统的程序,定义了以下字段:
private double salary;//最终工资
        private int No;//编号
        private int Workday;//月工作日天数
        private int Sex;//性别
        private string Name;//姓名
        private double BaseSalary;//基本工资
        private int Edu;//学历
        private int Workyear;//工龄
        private int year; //生日
        private int month;
       private int day;
我想将这些字段的信息保存至.txt文件用以下方法:
public void savefile()
        {
            const string filename = "data.txt";
            FileStream fs = File.Open(filename, FileMode.OpenOrCreate);
            
            BinaryWriter bw = new BinaryWriter(fs);
            //fileWriter.WriteLine("fsdfasdjfjskdl;fkjasldkfjals;djfl;sakdjflas;jdfl;fdsadfasdf");
            //fileWriter.Write(this.ToString());
            bw.Write(Program.n);
            bw.Write(this.No);
            bw.Write(this.Name);
            bw.Write(this.BaseSalary);
            bw.Write(this.Workday);
            bw.Write(this.Workyear);
            bw.Write(this.Sex);
            bw.Write(this.year);
            bw.Write(this.month);
            bw.Write(this.day);
            bw.Close();
            
            fs.Close();        }
可是如果这样只能一个个按顺序读出来!请问还有什么更好的办法吗?

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Xml.Serialization;
    using System.IO;namespace Serializiable
    {
        class Program
        {
            static void Main(string[] args)
            {
               
                TestEngineer engineer=new TestEngineer(100.00,0001,5,1,"Tom");
                FileStream stream = new FileStream("EngineersXML.xml", FileMode.Create);
                XmlSerializer serializer = new XmlSerializer(typeof(TestEngineer));//xml序列化只能序列实例中的共有字段和共有属性,要序列化所有要用binary
                serializer.Serialize(stream, engineer);
                //BinaryFormatter formatter = new BinaryFormatter();
                //formatter.Serialize(stream, engineer);
                stream.Close();
            }
        }
        [Serializable]
        public class TestEngineer
        {
            private double _salary;//最终工资 
            public double Salary
            {
                get { return _salary; }
                set { _salary = value; }
            }
            private int _no;//编号 
            public int No
            {
                get { return _no; }
                set { _no = value; }
            }
            private int _workday;//月工作日天数
            public int Workday
            {
                get { return _workday; }
                set { _workday = value; }
            }
     
            private int _sex;//性别 
            public int Sex
            {
                get { return _sex; }
                set { _sex = value; }
            }        private string _name;//姓名
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
                    public TestEngineer(double salary,int no,int workday,int sex,string name)
            {
                this.Salary = salary;
                this.No = no;
                this.Workday = workday;
                this.Sex = sex;
                this.Name = name; 
            }
            public TestEngineer()//XML;序列化必须有无参构造函数
            { 
            }    }
    }