xml文件的读写(Reading and writing XML files)

//读取本地的配置文件到界面       private void InitControlStatus()
        {
            MaterialModel<Station> materialModel = new MaterialModel<Station>();
            string filePath = ConfigurationModel.Instance.ConfigPath + "MaterialConfig.xml";
            materialModel = XMLSetting.XMLRead<MaterialModel<Station>>(filePath);

            if (materialModel != null && materialModel.CategoryConfig.Count > 0)
            {
                foreach (var item in materialModel.CategoryConfig)
                {
                    switch (item.MaterialType)
                    {
                        case "001":
                            txt_CathodeSN.Text = item.MaterialSN;
                            txt_CathodeSN.Enabled = item.TBoxEnable;
                            btn_CathodeUP.Enabled = item.BtnUPEnable;
                            btn_CathodeDown.Enabled = item.BtnDWNEnable;
                            SessionModel.CathodeStatus = item.IsUpdate;
                            break;
                        case "002":
                            txt_AnodeSN.Text = item.MaterialSN;
                            txt_AnodeSN.Enabled = item.TBoxEnable;
                            btn_AnodeUp.Enabled = item.BtnUPEnable;
                            btn_AnodeDown.Enabled = item.BtnDWNEnable;
                            SessionModel.AnodeStatus = item.IsUpdate;
                            break;
                        case "003":
                            txt_UpDiaphragmSN.Text = item.MaterialSN;
                            txt_UpDiaphragmSN.Enabled = item.TBoxEnable;
                            btn_TopUp.Enabled = item.BtnUPEnable;
                            btn_TopDown.Enabled = item.BtnDWNEnable;
                            SessionModel.TopStatus = item.IsUpdate;
                            break;
                        case "004":
                            txt_DownDiaphragmSN.Text = item.MaterialSN;
                            txt_DownDiaphragmSN.Enabled = item.TBoxEnable;
                            btn_BottomUp.Enabled = item.BtnUPEnable;
                            btn_BottomDown.Enabled = item.BtnDWNEnable;
                            SessionModel.BottomStatus = item.IsUpdate;
                            break;
                        case "005":
                            txt_TapeSN.Text = item.MaterialSN;
                            txt_TapeSN.Enabled = item.TBoxEnable;
                            btn_TapeUP.Enabled = item.BtnUPEnable;
                            btn_TapeDown.Enabled = item.BtnDWNEnable;
                            SessionModel.TapeStatus = item.IsUpdate;
                            break;
                        case "006":
                            txt_HightTempSN.Text = item.MaterialSN;
                            txt_HightTempSN.Enabled = item.TBoxEnable;
                            btn_HighTempUp.Enabled = item.BtnUPEnable;
                            btn_HighTempDown.Enabled = item.BtnDWNEnable;
                            SessionModel.HighTempStatus = item.IsUpdate;
                            break;
                    }
                }
            }
        }
Class MaterialModel类
public class MaterialModel<T>
    {
        public string ID { get; set; }
        /// <summary>
        /// 保存每一项物料的信息,如物料号,控件状态,上料状态
        /// </summary>
        public List<T> CategoryConfig { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public string CreateTime { get; set; }
    }

    public class Station
    {
        /// <summary>
        /// 物料类型,如:正极,负极,上隔膜,下隔膜,刻码胶,高温胶
        /// </summary>
        public string MaterialType { get; set; }
        /// <summary>
        /// 物料编码
        /// </summary>
        public string MaterialSN { get; set; }
        /// <summary>
        /// 文本框状态
        /// </summary>
        public bool TBoxEnable { get; set; }
        /// <summary>
        /// 上料按钮状态
        /// </summary>
        public bool BtnUPEnable { get; set; }
        /// <summary>
        /// 卸料按钮状态
        /// </summary>
        public bool BtnDWNEnable { get; set; }
        /// <summary>
        /// 是否成功上料
        /// </summary>
        public bool IsUpdate { get; set; }
    }

 class XMLSetting 类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace UCFileTools
{

    /// <summary>
    /// XML设置映射类型
    /// </summary>
    [XmlRoot("ECUF")]
    public class XMLSetting
    {
        private static string XMLPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Config.xml";

        #region 属性


        /// <summary>
        /// 生产模式:0、离线模式 1、采集模式 2、控制模式
        /// </summary>
        [XmlElement("ProdModel")]
        public string ProdModel
        {
            get;
            set;
        }
        #endregion

        #region 构造器


        private XMLSetting()
        {

        }

        public static XMLSetting GetInstance()
        {
            var result = new XMLSetting();
            if (File.Exists(XMLPath))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XMLPath); //加载XML文档
                var str = xmlDoc.InnerXml;
                result = Deserialize<XMLSetting>(str);
            }

            return result;

        }


        #endregion

        /// <summary>
        /// 添加读取方法 
        /// </summary>
        /// <typeparam name="T">类</typeparam>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public static T XMLRead<T>(string path)
        {
            T result = default(T);

            if (File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);  //加载XML文档
                var str = xmlDoc.InnerXml;
                result = Deserialize<T>(str);
            }

            return result;
        }


        #region  方法
        public void SaveSetting()
        {
            string str = Serialize(this);
            using (FileStream fs = new FileStream(XMLPath, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default))
                {
                    sw.WriteLine(str);
                }
            }


        }

        #region  序列化对象

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string Serialize(object obj, bool format = true)
        {
            if (obj == null)
            {
                return "";
            }
            Encoding gbk = Encoding.GetEncoding("GBK");
            XmlSerializer x = new XmlSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Encoding = gbk;
            setting.OmitXmlDeclaration = true;
            setting.Indent = format;
            using (XmlWriter writer = XmlWriter.Create(stream, setting))
            {
                XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
                xmlNamespace.Add("", "");  //去掉默认的命名空间
                x.Serialize(writer, obj, xmlNamespace);
            }
            string result = "<?xml version=\"1.0\" encoding=\"GBK\"?>\r\n";
            result += gbk.GetString(stream.ToArray());
            return result;
        }

        /// <summary>
        /// 保存XML文件代码
        /// </summary>
        /// <param name="obj">类对象数据</param>
        /// <param name="path">路径</param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static bool SerializeAndSaveFile(object obj, string path, bool format = true)
        {
            bool Flag = false;
            if (obj == null) return Flag;
            Encoding gbk = Encoding.GetEncoding("GBK");
            XmlSerializer x = XmlSerializer.FromTypes(new[] { obj.GetType() })[0];
            //XmlSerializer x = new XmlSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Encoding = gbk;
            setting.OmitXmlDeclaration = true;
            setting.Indent = format;
            using (XmlWriter writer = XmlWriter.Create(stream, setting))
            {
                XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
                xmlNamespace.Add("", "");
                x.Serialize(writer, obj, xmlNamespace);
            }
            string result = "<?xml version=\"1.0\" encoding=\"GBK\"?>\r\n" +
                gbk.GetString(stream.ToArray());

            //保存数据
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //string FullFilePath = path + "XmlFile" + DateTime.Now.ToString("yyyy-MM-dd HHmmssfff") + ".xml";
                string FullFilePath = path + "MaterialConfig" + ".xml";
                using (FileStream fs = new FileStream(FullFilePath, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                    {
                        sw.WriteLine(result);
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            return Flag;
        }


        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <param name="objType"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string xml)
        {
            T result = default(T);
            if (!string.IsNullOrEmpty(xml))
            {

                Type objType = typeof(T);
                MemoryStream stream = new MemoryStream(Encoding.GetEncoding("GBK").GetBytes(xml));
                result = (T)Deserialize(objType, stream);
            }
            return result;
        }
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <param name="objType"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static object Deserialize(Type objType, Stream xml)
        {
            //XmlSerializer x = new XmlSerializer(objType);
            XmlSerializer x=XmlSerializer.FromTypes(new[] { objType })[0];
            return x.Deserialize(xml);
        }

        #endregion


        #endregion

    }
}

  保存XML文件 的方法

    private void SaveMaterialConfig()
        {
            //Station station;
            MaterialModel<Station> materialModel = new MaterialModel<Station>();
            List<Station> lsSation = new List<Station>();

            lsSation.Add(new Station()
            {
                MaterialType = "001",
                MaterialSN = txt_CathodeSN.Text.Trim(),
                TBoxEnable = txt_CathodeSN.Enabled,
                BtnUPEnable=btn_CathodeUP.Enabled,
                BtnDWNEnable=btn_CathodeDown.Enabled,
                IsUpdate=SessionModel.CathodeStatus
            });
            lsSation.Add(new Station()
            {
                MaterialType = "002",
                MaterialSN = txt_AnodeSN.Text.Trim(),
                TBoxEnable = txt_AnodeSN.Enabled,
                BtnUPEnable = btn_AnodeUp.Enabled,
                BtnDWNEnable = btn_AnodeDown.Enabled,
                IsUpdate = SessionModel.AnodeStatus
            });
            lsSation.Add(new Station()
            {
                MaterialType = "003",
                MaterialSN = txt_UpDiaphragmSN.Text.Trim(),
                TBoxEnable = txt_UpDiaphragmSN.Enabled,
                BtnUPEnable = btn_TopUp.Enabled,
                BtnDWNEnable = btn_TopDown.Enabled,
                IsUpdate = SessionModel.TopStatus
            });
            lsSation.Add(new Station()
            {
                MaterialType = "004",
                MaterialSN = txt_DownDiaphragmSN.Text.Trim(),
                TBoxEnable = txt_DownDiaphragmSN.Enabled,
                BtnUPEnable = btn_BottomUp.Enabled,
                BtnDWNEnable = btn_BottomDown.Enabled,
                IsUpdate = SessionModel.BottomStatus
            });
            lsSation.Add(new Station()
            {
                MaterialType = "005",
                MaterialSN = txt_TapeSN.Text.Trim(),
                TBoxEnable = txt_TapeSN.Enabled,
                BtnUPEnable = btn_TapeUP.Enabled,
                BtnDWNEnable = btn_TapeDown.Enabled,
                IsUpdate = SessionModel.TapeStatus
            });
            lsSation.Add(new Station()
            {
                MaterialType = "006",
                MaterialSN = txt_HightTempSN.Text.Trim(),
                TBoxEnable = txt_HightTempSN.Enabled,
                BtnUPEnable = btn_HighTempUp.Enabled,
                BtnDWNEnable = btn_HighTempDown.Enabled,
                IsUpdate = SessionModel.HighTempStatus
            });

            materialModel.ID = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
            materialModel.CategoryConfig = lsSation;
            materialModel.CreateTime = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss");
            XMLSetting.SerializeAndSaveFile(materialModel,ConfigurationModel.Instance.ConfigPath);
        }

生成的XML文件为:

<?xml version="1.0" encoding="GBK"?>
<MaterialModelOfStation>
  <ID>2021-12-11 12:45:37 472</ID>
  <CategoryConfig>
    <Station>
      <MaterialType>001</MaterialType>
      <MaterialSN>ZJ994</MaterialSN>
      <TBoxEnable>true</TBoxEnable>
      <BtnUPEnable>true</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>false</IsUpdate>
    </Station>
    <Station>
      <MaterialType>002</MaterialType>
      <MaterialSN>FJ994</MaterialSN>
      <TBoxEnable>false</TBoxEnable>
      <BtnUPEnable>false</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>false</IsUpdate>
    </Station>
    <Station>
      <MaterialType>003</MaterialType>
      <MaterialSN>GM994</MaterialSN>
      <TBoxEnable>false</TBoxEnable>
      <BtnUPEnable>false</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>false</IsUpdate>
    </Station>
    <Station>
      <MaterialType>004</MaterialType>
      <MaterialSN>GM884</MaterialSN>
      <TBoxEnable>false</TBoxEnable>
      <BtnUPEnable>false</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>false</IsUpdate>
    </Station>
    <Station>
      <MaterialType>005</MaterialType>
      <MaterialSN>KMJ994</MaterialSN>
      <TBoxEnable>false</TBoxEnable>
      <BtnUPEnable>false</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>true</IsUpdate>
    </Station>
    <Station>
      <MaterialType>006</MaterialType>
      <MaterialSN>SWJ994</MaterialSN>
      <TBoxEnable>false</TBoxEnable>
      <BtnUPEnable>false</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>false</IsUpdate>
    </Station>
  </CategoryConfig>
  <CreateTime>2021-12-11 12:45:37</CreateTime>
</MaterialModelOfStation>

  项目文件,留个标记,以供参考。

————————
//读取本地的配置文件到界面       private void InitControlStatus()
        {
            MaterialModel<Station> materialModel = new MaterialModel<Station>();
            string filePath = ConfigurationModel.Instance.ConfigPath + "MaterialConfig.xml";
            materialModel = XMLSetting.XMLRead<MaterialModel<Station>>(filePath);

            if (materialModel != null && materialModel.CategoryConfig.Count > 0)
            {
                foreach (var item in materialModel.CategoryConfig)
                {
                    switch (item.MaterialType)
                    {
                        case "001":
                            txt_CathodeSN.Text = item.MaterialSN;
                            txt_CathodeSN.Enabled = item.TBoxEnable;
                            btn_CathodeUP.Enabled = item.BtnUPEnable;
                            btn_CathodeDown.Enabled = item.BtnDWNEnable;
                            SessionModel.CathodeStatus = item.IsUpdate;
                            break;
                        case "002":
                            txt_AnodeSN.Text = item.MaterialSN;
                            txt_AnodeSN.Enabled = item.TBoxEnable;
                            btn_AnodeUp.Enabled = item.BtnUPEnable;
                            btn_AnodeDown.Enabled = item.BtnDWNEnable;
                            SessionModel.AnodeStatus = item.IsUpdate;
                            break;
                        case "003":
                            txt_UpDiaphragmSN.Text = item.MaterialSN;
                            txt_UpDiaphragmSN.Enabled = item.TBoxEnable;
                            btn_TopUp.Enabled = item.BtnUPEnable;
                            btn_TopDown.Enabled = item.BtnDWNEnable;
                            SessionModel.TopStatus = item.IsUpdate;
                            break;
                        case "004":
                            txt_DownDiaphragmSN.Text = item.MaterialSN;
                            txt_DownDiaphragmSN.Enabled = item.TBoxEnable;
                            btn_BottomUp.Enabled = item.BtnUPEnable;
                            btn_BottomDown.Enabled = item.BtnDWNEnable;
                            SessionModel.BottomStatus = item.IsUpdate;
                            break;
                        case "005":
                            txt_TapeSN.Text = item.MaterialSN;
                            txt_TapeSN.Enabled = item.TBoxEnable;
                            btn_TapeUP.Enabled = item.BtnUPEnable;
                            btn_TapeDown.Enabled = item.BtnDWNEnable;
                            SessionModel.TapeStatus = item.IsUpdate;
                            break;
                        case "006":
                            txt_HightTempSN.Text = item.MaterialSN;
                            txt_HightTempSN.Enabled = item.TBoxEnable;
                            btn_HighTempUp.Enabled = item.BtnUPEnable;
                            btn_HighTempDown.Enabled = item.BtnDWNEnable;
                            SessionModel.HighTempStatus = item.IsUpdate;
                            break;
                    }
                }
            }
        }
Class MaterialModel类
public class MaterialModel<T>
    {
        public string ID { get; set; }
        /// <summary>
        /// 保存每一项物料的信息,如物料号,控件状态,上料状态
        /// </summary>
        public List<T> CategoryConfig { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public string CreateTime { get; set; }
    }

    public class Station
    {
        /// <summary>
        /// 物料类型,如:正极,负极,上隔膜,下隔膜,刻码胶,高温胶
        /// </summary>
        public string MaterialType { get; set; }
        /// <summary>
        /// 物料编码
        /// </summary>
        public string MaterialSN { get; set; }
        /// <summary>
        /// 文本框状态
        /// </summary>
        public bool TBoxEnable { get; set; }
        /// <summary>
        /// 上料按钮状态
        /// </summary>
        public bool BtnUPEnable { get; set; }
        /// <summary>
        /// 卸料按钮状态
        /// </summary>
        public bool BtnDWNEnable { get; set; }
        /// <summary>
        /// 是否成功上料
        /// </summary>
        public bool IsUpdate { get; set; }
    }

 class XMLSetting 类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace UCFileTools
{

    /// <summary>
    /// XML设置映射类型
    /// </summary>
    [XmlRoot("ECUF")]
    public class XMLSetting
    {
        private static string XMLPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Config.xml";

        #region 属性


        /// <summary>
        /// 生产模式:0、离线模式 1、采集模式 2、控制模式
        /// </summary>
        [XmlElement("ProdModel")]
        public string ProdModel
        {
            get;
            set;
        }
        #endregion

        #region 构造器


        private XMLSetting()
        {

        }

        public static XMLSetting GetInstance()
        {
            var result = new XMLSetting();
            if (File.Exists(XMLPath))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XMLPath); //加载XML文档
                var str = xmlDoc.InnerXml;
                result = Deserialize<XMLSetting>(str);
            }

            return result;

        }


        #endregion

        /// <summary>
        /// 添加读取方法 
        /// </summary>
        /// <typeparam name="T">类</typeparam>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public static T XMLRead<T>(string path)
        {
            T result = default(T);

            if (File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);  //加载XML文档
                var str = xmlDoc.InnerXml;
                result = Deserialize<T>(str);
            }

            return result;
        }


        #region  方法
        public void SaveSetting()
        {
            string str = Serialize(this);
            using (FileStream fs = new FileStream(XMLPath, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default))
                {
                    sw.WriteLine(str);
                }
            }


        }

        #region  序列化对象

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string Serialize(object obj, bool format = true)
        {
            if (obj == null)
            {
                return "";
            }
            Encoding gbk = Encoding.GetEncoding("GBK");
            XmlSerializer x = new XmlSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Encoding = gbk;
            setting.OmitXmlDeclaration = true;
            setting.Indent = format;
            using (XmlWriter writer = XmlWriter.Create(stream, setting))
            {
                XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
                xmlNamespace.Add("", "");  //去掉默认的命名空间
                x.Serialize(writer, obj, xmlNamespace);
            }
            string result = "<?xml version=\"1.0\" encoding=\"GBK\"?>\r\n";
            result += gbk.GetString(stream.ToArray());
            return result;
        }

        /// <summary>
        /// 保存XML文件代码
        /// </summary>
        /// <param name="obj">类对象数据</param>
        /// <param name="path">路径</param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static bool SerializeAndSaveFile(object obj, string path, bool format = true)
        {
            bool Flag = false;
            if (obj == null) return Flag;
            Encoding gbk = Encoding.GetEncoding("GBK");
            XmlSerializer x = XmlSerializer.FromTypes(new[] { obj.GetType() })[0];
            //XmlSerializer x = new XmlSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Encoding = gbk;
            setting.OmitXmlDeclaration = true;
            setting.Indent = format;
            using (XmlWriter writer = XmlWriter.Create(stream, setting))
            {
                XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
                xmlNamespace.Add("", "");
                x.Serialize(writer, obj, xmlNamespace);
            }
            string result = "<?xml version=\"1.0\" encoding=\"GBK\"?>\r\n" +
                gbk.GetString(stream.ToArray());

            //保存数据
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //string FullFilePath = path + "XmlFile" + DateTime.Now.ToString("yyyy-MM-dd HHmmssfff") + ".xml";
                string FullFilePath = path + "MaterialConfig" + ".xml";
                using (FileStream fs = new FileStream(FullFilePath, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                    {
                        sw.WriteLine(result);
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            return Flag;
        }


        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <param name="objType"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string xml)
        {
            T result = default(T);
            if (!string.IsNullOrEmpty(xml))
            {

                Type objType = typeof(T);
                MemoryStream stream = new MemoryStream(Encoding.GetEncoding("GBK").GetBytes(xml));
                result = (T)Deserialize(objType, stream);
            }
            return result;
        }
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <param name="objType"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static object Deserialize(Type objType, Stream xml)
        {
            //XmlSerializer x = new XmlSerializer(objType);
            XmlSerializer x=XmlSerializer.FromTypes(new[] { objType })[0];
            return x.Deserialize(xml);
        }

        #endregion


        #endregion

    }
}

Method of saving XML file

    private void SaveMaterialConfig()
        {
            //Station station;
            MaterialModel<Station> materialModel = new MaterialModel<Station>();
            List<Station> lsSation = new List<Station>();

            lsSation.Add(new Station()
            {
                MaterialType = "001",
                MaterialSN = txt_CathodeSN.Text.Trim(),
                TBoxEnable = txt_CathodeSN.Enabled,
                BtnUPEnable=btn_CathodeUP.Enabled,
                BtnDWNEnable=btn_CathodeDown.Enabled,
                IsUpdate=SessionModel.CathodeStatus
            });
            lsSation.Add(new Station()
            {
                MaterialType = "002",
                MaterialSN = txt_AnodeSN.Text.Trim(),
                TBoxEnable = txt_AnodeSN.Enabled,
                BtnUPEnable = btn_AnodeUp.Enabled,
                BtnDWNEnable = btn_AnodeDown.Enabled,
                IsUpdate = SessionModel.AnodeStatus
            });
            lsSation.Add(new Station()
            {
                MaterialType = "003",
                MaterialSN = txt_UpDiaphragmSN.Text.Trim(),
                TBoxEnable = txt_UpDiaphragmSN.Enabled,
                BtnUPEnable = btn_TopUp.Enabled,
                BtnDWNEnable = btn_TopDown.Enabled,
                IsUpdate = SessionModel.TopStatus
            });
            lsSation.Add(new Station()
            {
                MaterialType = "004",
                MaterialSN = txt_DownDiaphragmSN.Text.Trim(),
                TBoxEnable = txt_DownDiaphragmSN.Enabled,
                BtnUPEnable = btn_BottomUp.Enabled,
                BtnDWNEnable = btn_BottomDown.Enabled,
                IsUpdate = SessionModel.BottomStatus
            });
            lsSation.Add(new Station()
            {
                MaterialType = "005",
                MaterialSN = txt_TapeSN.Text.Trim(),
                TBoxEnable = txt_TapeSN.Enabled,
                BtnUPEnable = btn_TapeUP.Enabled,
                BtnDWNEnable = btn_TapeDown.Enabled,
                IsUpdate = SessionModel.TapeStatus
            });
            lsSation.Add(new Station()
            {
                MaterialType = "006",
                MaterialSN = txt_HightTempSN.Text.Trim(),
                TBoxEnable = txt_HightTempSN.Enabled,
                BtnUPEnable = btn_HighTempUp.Enabled,
                BtnDWNEnable = btn_HighTempDown.Enabled,
                IsUpdate = SessionModel.HighTempStatus
            });

            materialModel.ID = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
            materialModel.CategoryConfig = lsSation;
            materialModel.CreateTime = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss");
            XMLSetting.SerializeAndSaveFile(materialModel,ConfigurationModel.Instance.ConfigPath);
        }

The generated XML file is:

<?xml version="1.0" encoding="GBK"?>
<MaterialModelOfStation>
  <ID>2021-12-11 12:45:37 472</ID>
  <CategoryConfig>
    <Station>
      <MaterialType>001</MaterialType>
      <MaterialSN>ZJ994</MaterialSN>
      <TBoxEnable>true</TBoxEnable>
      <BtnUPEnable>true</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>false</IsUpdate>
    </Station>
    <Station>
      <MaterialType>002</MaterialType>
      <MaterialSN>FJ994</MaterialSN>
      <TBoxEnable>false</TBoxEnable>
      <BtnUPEnable>false</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>false</IsUpdate>
    </Station>
    <Station>
      <MaterialType>003</MaterialType>
      <MaterialSN>GM994</MaterialSN>
      <TBoxEnable>false</TBoxEnable>
      <BtnUPEnable>false</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>false</IsUpdate>
    </Station>
    <Station>
      <MaterialType>004</MaterialType>
      <MaterialSN>GM884</MaterialSN>
      <TBoxEnable>false</TBoxEnable>
      <BtnUPEnable>false</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>false</IsUpdate>
    </Station>
    <Station>
      <MaterialType>005</MaterialType>
      <MaterialSN>KMJ994</MaterialSN>
      <TBoxEnable>false</TBoxEnable>
      <BtnUPEnable>false</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>true</IsUpdate>
    </Station>
    <Station>
      <MaterialType>006</MaterialType>
      <MaterialSN>SWJ994</MaterialSN>
      <TBoxEnable>false</TBoxEnable>
      <BtnUPEnable>false</BtnUPEnable>
      <BtnDWNEnable>true</BtnDWNEnable>
      <IsUpdate>false</IsUpdate>
    </Station>
  </CategoryConfig>
  <CreateTime>2021-12-11 12:45:37</CreateTime>
</MaterialModelOfStation>

Project documents, leave a mark for reference.