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

 

posted @ 2023-06-06 09:27  懒树懒  阅读(56)  评论(0编辑  收藏  举报