将对象XML序列化为XML文件/反序列化XML文件为对象

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using NPOI.HSSF.UserModel;
using NPOI.XSSF;
using NPOI.XSSF.UserModel;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //XML序列化对象集合
            //Department dep = new Department();
            //dep.Name = "事业部";
            //dep.Teams = new List<Team>();
            //dep.Teams.Add(new Team("Test", "测试团队"));
            //dep.Teams.Add(new Team("Develop", "开发团队"));

            //dep["Test"].Persons = new List<Person>();
            //dep["Test"].Persons.Add(new Person("dwf", 22, "程序", "测试工程师"));
            //dep["Test"].Persons.Add(new Person("WL", 22, "代码", "测试工程师"));

            //dep["Develop"].Persons = new List<Person>();
            //dep["Develop"].Persons.Add(new Person("dwf", 22, "程序", "开发工程师"));
            //dep["Develop"].Persons.Add(new Person("WL", 22, "代码", "高级开发工程师"));

            //XmlSerializer serializer = new XmlSerializer(dep.GetType());
            //TextWriter writer = new StreamWriter("Department.xml");

            //serializer.Serialize(writer, dep);
            //writer.Close();

            //Machine = "WB01", Type = "BLT&Tilting", Project = "S010", Sample = 5 
            //FileFormat file = new FileFormat();
            //file.Name = "IT";

            //file.Modules = new List<Module>();

            //file.Modules.Add(new Module("WB01", "BLT&Tilting", "S010", 5,"Format1"));
            //file.Modules.Add(new Module("WB02", "BTT&Tilting", "T4061", 5, "Format1"));

            //XmlSerializer serializer = new XmlSerializer(file.GetType());
            //TextWriter writer = new StreamWriter("Formats.xml");

            //serializer.Serialize(writer, file);
            //writer.Close();

            //反序列化XML数据为对象集合
            //XmlSerializer serializer = new XmlSerializer(typeof(Department));
            //FileStream stream = new FileStream("Department.xml", FileMode.Open);
            //Department dep = (Department)serializer.Deserialize(stream);
            //stream.Close();

            XmlSerializer serializer = new XmlSerializer(typeof(FileFormat));
            FileStream stream = new FileStream("Formats.xml", FileMode.Open);
            FileFormat obj = (FileFormat)serializer.Deserialize(stream);
            stream.Close();
        }
    }

    public class FileFormat
    {
        [XmlAttribute]
        public string Name { get; set; }

        public List<Module> Modules;

        public Module this[string Name]
        {
            get
            {
                Module m = null;

                foreach (Module te in Modules)
                {
                    if (string.Compare(te.Name, Name) == 0)
                    {
                        m = te;
                        break;
                    }
                }
                return m;
            }
        }
    }

    public class Module
    {
        public string Name;
        public string Machine;
        public string Type;
        public string Project;
        public int Sample;

        public Module()
        { }

        public Module(string machine,string type,string project ,int sample, string name)
        {
            this.Machine = machine;
            this.Type = type;
            this.Project = project;
            this.Sample = sample;
            this.Name = name;
        }
    }

    public class Department
    {
        public Department()
        {
        }
        [XmlAttribute]
        public string Name;
        public List<Team> Teams;
        public Team this[string Name]
        {
            get
            {
                Team t = null;
                foreach (Team te in Teams)
                {
                    if (string.Compare(te.Name, Name) == 0)
                    {
                        t = te;
                        break;
                    }
                }
                return t;
            }
        }
    }

    public class Team
    {
        public Team()
        {

        }
        public Team(string Name, string Title)
        {
            this.Name = Name;
            this.Title = Title;
        }
        public string Name;
        public string Title;
        public List<Person> Persons;

    }

    public class Person
    {
        public Person()
        {

        }
        public Person(string Name, int Age, string Hobby, string Station)
        {
            this.Name = Name;
            this.Age = Age;
            this.Hobby = Hobby;
            this.Station = Station;
        }
        public string Name;
        public int Age;
        public string Hobby;
        public string Station;
    }
}