为了构建如下图所示的xml文件 在这个文件中CompanyConfig为该XML的根节点,它的里面由多个DepartmentConfig节点组成,在DepartmentConfig节点中又包括PersonInfo节点,PersonInfo中又包括姓名和职位。
下面首先我们要做的是创建与该XML相对应的对象,然后把对象转换为上述XML(序列化),或者把上述XML转换为对象(反序列化)。
首先,我们先创建一个最底层的人员信息类(PersonInfo类),该类里包含人员姓名和职位
/// <summary> /// 人员信息 /// </summary> public class PersonInfo { /// <summary> /// 获取或设置人员姓名 /// </summary> [XmlElement(ElementName = "姓名")] //设置需要序列化的标签名称 public string Name { get; set; } [XmlElement (ElementName ="职位")] public string Position { get; set; } }接下来创建这一层的上一级类 部门信息类(DepartmentConfig类),该类中包含部门的名称以及PersonInfo类的集合。
/// <summary> /// 部门信息 /// </summary> public class DepartmentConfig { private List<PersonInfo> mPersonInfo; /// <summary> /// 获取或设置部门名称 /// </summary> [XmlAttribute(AttributeName = "部门名称")] //设置标签属性的名称 public string DepartmentName { get; set; } /// <summary> /// 获取或设置人员列 /// </summary> [XmlArray (ElementName = "PersonList")] public List<PersonInfo> PersonList { get { return mPersonInfo ??(mPersonInfo =new List<PersonInfo>() ); } set { mPersonInfo = value; } } }接下来创建最外层了类 公司信息类(CompanyConfig类),该类包含公司名称和DepartmentConfig类的集合。并在该类中进行初始化。
public class CompanyConfig { private List<DepartmentConfig> mDepartmentConfig; private readonly string SAVE_PATH; private string mCompangName = "青岛秀山移动测量有限公司"; /// <summary> /// 获取或设置公司名称 /// </summary> [XmlAttribute(AttributeName = "公司名称")] public string CompanyName { get { return mCompangName; } set { mCompangName = value; } } /// <summary> /// 获取或设置部门配置信息 /// </summary> [XmlArray(ElementName = "DepartmentList")] public List<DepartmentConfig> DepartmentList { get { return mDepartmentConfig ?? (mDepartmentConfig = new List<DepartmentConfig>()); } set { mDepartmentConfig = value; } } //构造函数进行初始化 public CompanyConfig() { SAVE_PATH = (@"F:\练习\SerializeXML\CompanyConfig.xml"); //实例化人员信息 PersonInfo pPerson0 = new PersonInfo { Name = "侯**", Position = "部门经理" }; PersonInfo pPerson1 = new PersonInfo { Name = "贾**", Position = "部门副经理" }; PersonInfo pPerson2 = new PersonInfo { Name = "张*", Position = "员工" }; PersonInfo pPerson3 = new PersonInfo { Name = "王**", Position = "部门经理" }; PersonInfo pPerson4 = new PersonInfo { Name = "杨*", Position = "研究生" }; PersonInfo pPerson5 = new PersonInfo { Name = "丁**", Position = "研究生" }; PersonInfo pPerson6 = new PersonInfo { Name = "俞**", Position = "博士生" }; PersonInfo pPerson7 = new PersonInfo { Name = "马*", Position = "研究生" }; PersonInfo pPerson8 = new PersonInfo { Name = "程**", Position = "研究生" }; PersonInfo pPerson9 = new PersonInfo { Name = "冯**", Position = "博士生" }; PersonInfo pPerson10 = new PersonInfo { Name = "季*", Position = "研究生" }; PersonInfo pPerson11 = new PersonInfo { Name = "王**", Position = "研究生" }; //配置软件部名称及人员信息 DepartmentConfig pSoftwareDepartment = new DepartmentConfig(); pSoftwareDepartment.DepartmentName = "软件部"; pSoftwareDepartment.PersonList.Add(pPerson0 ); pSoftwareDepartment.PersonList.Add(pPerson1); pSoftwareDepartment.PersonList.Add(pPerson2); //配置技术服务部名称及人员信息 DepartmentConfig pTechnicalServiceDepartment = new DepartmentConfig(); pTechnicalServiceDepartment.DepartmentName = "技术服务部"; pTechnicalServiceDepartment.PersonList.Add(pPerson3); pTechnicalServiceDepartment.PersonList.Add(pPerson4); pTechnicalServiceDepartment.PersonList.Add(pPerson5); //配置检校部名称及人员信息 DepartmentConfig pCalibrationDepartment = new DepartmentConfig(); pCalibrationDepartment.DepartmentName = "检校部"; pCalibrationDepartment.PersonList.Add(pPerson6); pCalibrationDepartment.PersonList.Add(pPerson7); pCalibrationDepartment.PersonList.Add(pPerson8); //配置海测部名称及人员信息 DepartmentConfig pMarineSurveyDepartment = new DepartmentConfig(); pMarineSurveyDepartment.DepartmentName = "海测部"; pMarineSurveyDepartment.PersonList.Add(pPerson9); pMarineSurveyDepartment.PersonList.Add(pPerson10); pMarineSurveyDepartment.PersonList.Add(pPerson11); //配置公司名称及部门信息 mDepartmentConfig = new List<DepartmentConfig>(); mDepartmentConfig.Add(pSoftwareDepartment); mDepartmentConfig.Add(pTechnicalServiceDepartment); mDepartmentConfig.Add(pCalibrationDepartment); mDepartmentConfig.Add(pMarineSurveyDepartment); } /// <summary> /// 序列化公共文件配置 /// </summary> public void Serialize() { if (File.Exists(SAVE_PATH) == false) { using (File.Create(SAVE_PATH)) { } } XmlSerialize<CompanyConfig> pXmlSerialize = new XmlSerialize<CompanyConfig>(); pXmlSerialize.Serialize(this, SAVE_PATH); } /// <summary> /// 反序列化文件公共配置 /// </summary> /// <returns></returns> public CompanyConfig Deserialize() { if (File.Exists(SAVE_PATH) == false) { Serialize(); } XmlSerialize<CompanyConfig> pXmlSerialize = new XmlSerialize<CompanyConfig>(); CompanyConfig pCompanyConfig = pXmlSerialize.DeSerialize(SAVE_PATH); return pCompanyConfig; } }XmlSerialize方法需要添加引用
此时在主程序里序列化就能在指定路径得到相应的文件
static void Main(string[] args) { CompanyConfig pCompanyConfig = new CompanyConfig(); pCompanyConfig.Serialize(); }