【C#】ini文件、xml文件操作汇总

一、ini文件

1、创建、读写

https://www.cnblogs.com/xmy-007/p/6400221.html

二、xml文件

1、读写

      /// <summary>
        /// 加载xml文件保存到指定类
        /// </summary>
        public static bool LoadXml(WaveData_Class testObj, string Fliepath)
        {
            bool res = false;
            testObj = new WaveData_Class();
            try
            {
                using (FileStream fs = new FileStream(Fliepath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(WaveData_Class));
                    testObj = (WaveData_Class)xs.Deserialize(fs);
                    fs.Close();
                }

                res = true;
            }
            catch (System.Exception ex)
            {
                res = false;
                MessageBox.Show(ex.ToString(), "ERROR",  MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return res;
        }

        /// <summary>
        /// 存储指定类到xml文件
        /// </summary>
        /// <param name="testObj"></param>
        /// <param name="Fliepath"></param>
        /// <returns></returns>
        public static bool FileSaveXml(WaveData_Class testObj, string Fliepath)
        {

            bool res = false;
            try
            {
                FileStream save_write;
                save_write = File.Open(Fliepath, System.IO.FileMode.OpenOrCreate);
                XmlSerializer bf = new XmlSerializer(typeof(WaveData_Class));
                bf.Serialize(save_write, testObj);
                save_write.Close();
                res = true;
            }
            catch (System.Exception ex)
            {
                res = false;
                MessageBox.Show(ex.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return res;
        }

        /// <summary>
        /// 修改xml文件指定节点参数并保存
        /// </summary>
        public static void WriteXmlNode(string path, double value, string testName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlElement rootElem = xmlDoc.DocumentElement; //获取根节点
            XmlNode node1 = rootElem;
            foreach (XmlNode nd1 in node1)
            {
                XmlElement element1 = (XmlElement)nd1;
                if (element1.Name == "Judge")
                {
                    XmlNodeList node2 = element1.ChildNodes;//获取所有子节点 
                    foreach (XmlNode nd2 in node2)//遍历 
                    {
                        XmlElement element2 = (XmlElement)nd2;//转换类型 
                        string valuestr = element2.GetAttribute("Value");
                        if (valuestr.Contains(testName))
                        {
                            XmlNodeList node3 = element2.ChildNodes;//继续获取子节点的所有子节点 
                            foreach (XmlNode nd3 in node3)//遍历 
                            {
                                XmlElement element3 = (XmlElement)nd3;
                                if (element3.GetAttribute("Name") == "参数")//如果找到 
                                {
                                    element3.SetAttribute("Value", value.ToString());//则修改
                                }
                            }
                        }
                    }
                }
            }
            xmlDoc.Save(path);//保存
        }

        /// <summary>
        /// 按节点获取xml文件的数据
        /// </summary>
        /// <param name="path"></param>
        public static void ReadXmlNode(string path)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlElement rootElem = xmlDoc.DocumentElement; //获取根节点
            XmlNode node1 = rootElem;
            foreach (XmlNode nd1 in node1)
            {
                XmlElement element1 = (XmlElement)nd1; //转换类型 
                XmlNodeList node2 = element1.ChildNodes;//获取所有子节点 
                foreach (XmlNode nd2 in node2)//遍历 
                {
                    XmlElement element2 = (XmlElement)nd2;
                    string namestr = element2.GetAttribute("Name");
                    string valuestr = element2.GetAttribute("Value");
                    XmlNodeList node3 = element2.ChildNodes;//继续获取子节点的所有子节点
                    if (node3.Count > 0)
                    {
                        foreach (XmlNode nd3 in node3)
                        {
                            XmlElement element3 = (XmlElement)nd3;
                            string nodeNamestr = element3.GetAttribute("Name");
                            string nodeValuestr = element3.GetAttribute("Value");
                        }
                    }
                    else
                    {

                    }
                }
            }
            xmlDoc.Save(path);//保存
        }

 

2、读xml注释

https://www.cnblogs.com/langhua/p/4211669.html

 

posted @ 2022-09-26 15:54  不溯流光  阅读(160)  评论(0编辑  收藏  举报