C#序列化反序列化读写XML文件

本文链接:https://blog.csdn.net/qq_43393323/article/details/90577432

为了构建如下图所示的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; }
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

接下来创建这一层的上一级类 部门信息类(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;
        }
    }
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33

接下来创建最外层了类 公司信息类(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;
    }
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120

XmlSerialize方法需要添加引用

此时在主程序里序列化就能在指定路径得到相应的文件

    static void Main(string[] args)
    {
        CompanyConfig pCompanyConfig = new CompanyConfig();
        pCompanyConfig.Serialize();
    }

    1
    2
    3
    4
    5


————————————————
版权声明:本文为CSDN博主「(Mr.Sun )」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43393323/article/details/90577432

posted @ 2019-11-21 10:09  Net-Spider  阅读(346)  评论(0)    收藏  举报