C#操作XML之保存参数

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using System.Xml.Serialization; namespace XMLtest { public class ParamHelper { public List<RecipeMode> DeRecipeMode(string path) { List<RecipeMode> list = new List<RecipeMode>(); XmlSerializer xml=new XmlSerializer(typeof(List<RecipeMode>)); try { using (FileStream reader=new FileStream("RecipeMode.xml",FileMode.Open)) { list = (List<RecipeMode>)xml.Deserialize(reader); } return list; } catch (Exception) { throw; } } public bool AddMode(string path,List<RecipeMode> listMode,RecipeMode recipemode) { if (recipemode==null) { return false; } if (CheckContainMode(listMode,recipemode.RecipeName)) { return false; } listMode.Add(recipemode); SaveMode(path,listMode); return true; } public bool SaveMode(string path, List<RecipeMode> listMode) { if (listMode==null) { return false; } XmlSerializer xml=new XmlSerializer(typeof(List<RecipeMode>)); using (FileStream fs=new FileStream(path, FileMode.Create)) { XmlWriter writer = new XmlTextWriter(fs, Encoding.UTF8); xml.Serialize(writer, listMode); } return true; } public bool CheckContainMode(List<RecipeMode> listMode, string recipeName) { foreach (var item in listMode) { if (item.RecipeName==recipeName) { return true; } } return false; } public bool DelMode(string path, List<RecipeMode> listMode, string recipeName) { if (listMode==null) { return false; } int index=0; foreach (var item in listMode) { if (item.RecipeName == recipeName) { break; } index++; } listMode.RemoveAt(index); SaveMode(path, listMode); return true; } public bool ModifyMode(string path, List<RecipeMode> listMode, string recipeName,string recipeProgram,string PT) { if (listMode == null) { return false; } if (!CheckContainMode(listMode, recipeName)) { return false; } int index = 0; foreach (var item in listMode) { if (item.RecipeName == recipeName) { listMode[index].LaserProgram = recipeProgram; double laserPT = 0; bool b = double.TryParse(PT, out laserPT); listMode[index].RecipeLaserPT = b ? laserPT : 0; break; } index++; } SaveMode(path, listMode); return true; } } [XmlRoot(ElementName ="Config")] public class RecipeMode { [XmlAttribute(AttributeName = "RecipeName")] public string RecipeName { get; set; } [XmlAttribute(AttributeName = "RecipeLaserPT")] public double RecipeLaserPT { get; set; } [XmlAttribute(AttributeName = "LaserProgram")] public string LaserProgram { get; set; } } public enum PrintDataEnum { ORDER_NUMBER, SERIAL_NUMBER, PROD_NUMBER, PROD_SPEC, MATMAU1, MATMAU2, MATMAU3, MATMAU4, MATMAU5, CUST_SN, PART_VERSION, PART_STATE, MATATTR1, MATATTR2, MATATTR3, MATATTR4, MATATTR5 } }
重复就是力量,数量堆死质量
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)