首页 > 编程语言 >C#操作XML之保存参数

C#操作XML之保存参数

时间:2023-06-06 09:46:59浏览次数:58  
标签:XML return string C# List listMode 参数 using public

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
    }
}
View Code

 

标签:XML,return,string,C#,List,listMode,参数,using,public
From: https://www.cnblogs.com/HomeSapiens/p/17459641.html

相关文章

  • P2973 [USACO10HOL] Driving Out the Piggies G 臭气弹
    题目链接题目:奶牛们建立了一个随机化的臭气炸弹来驱逐猪猡。猪猡的文明包含1到N(2<=N<=300)一共N个猪城。这些城市由M(1<=M<=44,850)条由两个不同端点A_j和B_j(1<=A_j<=N;1<=B_j<=N)表示的双向道路连接。保证城市1至少连接一个其它的城市。一开始臭气弹会被......
  • Docker安装MS SQL Server并使用Navicat远程连接
    思维导航MSSQLServer简介基于Ubuntu的MicrosoftSQLServer官方镜像拉取Microsoft SQLServer2022容器镜像验证mssql镜像是否成功拉取到本地创建并运行一个mssql容器注意大坑使用Docker部署SqlServer容器后过几秒就停止了验证容器是否创建成功设置Docker主机防......
  • 如何使用TypeScript和Styled-Components构建图像轮播组件
    近年来,OTT(over-the-top)视频流媒体平台变得更加创新和易于使用。在他们的用户界面中,电影和连续剧的标题排列得清晰可见。在本教程中,我将指导您完成创建图像轮播组件的过程,该组件看起来就像您在许多OTT平台(想想Netflix)上看到的一样。我们将从创建原子组件开始,例如Tags、Descrip......
  • NET Core CLI命令小结
    dotnetrundotnetrun--urls=http://*:8080//urls指定ip和端口dotnetwatchrun//watch监听程序修改,一旦修改则重新启动dotnetwatchrun--urls=http://*:8080dotnetrun-pNETCoreCLI.csproj//一个“-”是简称的pdotnetrun--projectNETCoreCLI.csproj//两个“-”是全......
  • Git 的SSL certificate problem: unable to get local issuer certificate问题
    D:\temp>gitclonehttps://github.com/xxxxxx/yyyyyy.gitCloninginto'yyyyyy'...fatal:unabletoaccess'https://github.com/xxxxxx/yyyyyy.git/':SSLcertificateproblem:unabletogetlocalissuercertificate处理方法:D:\temp>gitco......
  • ORA-01555:snapshot too old: rollback segment number X with name "XXXX" too small
    ORA-01555:snapshottooold:rollbacksegmentnumberXwithname"XXXX"toosmall在查询快照的时候select*fromtesttableasoftimestampto_timestamp('2023-04-0322:00:00','yyyy-mm-ddhh24:mi:ss')提示错误ORA-01555:snapshottooold:r......
  • nginx开启websocket
    http{map$http_upgrade$connection_upgrade{defaultupgrade;''close;}upstreamwebsocket{server192.168.100.10:8010;}server{listen8020;location/{proxy_p......
  • LKT6830C安全MCU(一):资源介绍
    LKT6830C是以32位高安全CPU为基础开发的高性能、高安全性的MCU主控。LKT6830C除了具有SPI、IIC、UART、GPIO等常用外设接口外同时芯片内置定时器、看门狗、PWM、DMA等功能。在充分保证芯片内部程序存储和运行安全的前提下,满足客户各种基本应用开发的要求。LKT6830C支持多种国际......
  • Jenkins+Docker 一键自动化部署项目!【转】
    本文章实现最简单全面的Jenkins+docker+springboot一键自动部署项目,步骤齐全,少走坑路。环境:centos7+git(gitee)简述实现步骤:在docker安装jenkins,配置jenkins基本信息,利用Dockerfile和shell脚本实现项目自动拉取打包并运行。安装dockerdocker安装社区版本CE确保yum包更新......
  • uniCloud笔记
    uniCloud笔记结合:uni-admin实现后台的云管理,schema2code辅助自动生成代码(只需要定义好表结构)云函数云函数,是将本地写好的函数上传到云端,在云端的node.js的环境中运行。可以在本地的页面中在生命周期函数(钩子函数)中调用云函数如下://在组件/页面加载时,调用云函数的回调函数on......