C#如何写入和读取xml文件

发布时间:2020年05月24日 阅读:143 次

C#开发的程序在保存程序配置信息时,可以将信息保存到xml文件中。

如何将配置信息保存到app.config文件中。

首先在Microsoft Visual Studio的项目中引入System.configuration


读取XML信息代码参考如下

  private void readconfig()
        {
            labeltime.Text = System.Configuration.ConfigurationManager.AppSettings["time"];
            label22.Text = System.Configuration.ConfigurationManager.AppSettings["tinum"];
            label_timesall.Text= System.Configuration.ConfigurationManager.AppSettings["cishu"];
            string Isxiaoshu= System.Configuration.ConfigurationManager.AppSettings["Isxiaoshu"];
            if (Isxiaoshu == "Y")
            {
                radioButton4.Checked = true;
                radioButton3.Checked = false;
            }
            if (Isxiaoshu == "N")
            {
                radioButton3.Checked = true;
                radioButton4.Checked = false;
            }

            numericUpDown1.Value = decimal.Parse( System.Configuration.ConfigurationManager.AppSettings["a"]);
            numericUpDown2.Value = decimal.Parse(System.Configuration.ConfigurationManager.AppSettings["b"]);
            numericUpDown3.Value= decimal.Parse(System.Configuration.ConfigurationManager.AppSettings["tinum"]);
            numericUpDown_timeset.Value= decimal.Parse(System.Configuration.ConfigurationManager.AppSettings["time"]);
        }


写入XML信息代码参考如下

try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.StartupPath + "\\小键盘.exe.config");//程序名字.exe.config
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//appSettings");

                string AppKey = "Isxiaoshu";//字段名称
                string isornot="Y";
                xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
                if (radioButton3.Checked == true)
                {
                    isornot = "N";
                }
                if (radioButton4.Checked == true)
                {
                    isornot = "Y";
                }
                if (xElem1 != null)
                {
                    xElem1.SetAttribute("value", isornot);//设置新内容
                }

                AppKey = "a";
                xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
                if (xElem1 != null)
                {
                    xElem1.SetAttribute("value", numericUpDown1.Value.ToString());
                }

                AppKey = "b";
                xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
                if (xElem1 != null)
                {
                    xElem1.SetAttribute("value", numericUpDown2.Value.ToString());
                }


                AppKey = "tinum";
                xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
                if (xElem1 != null)
                {
                    xElem1.SetAttribute("value", numericUpDown3.Value.ToString());
                }

                AppKey = "time";
                xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
                if (xElem1 != null)
                {
                    xElem1.SetAttribute("value", numericUpDown_timeset.Value.ToString());
                }

                xDoc.Save(Application.StartupPath + "\\小键盘.exe.config");//保存


                MessageBox.Show("设置信息保存成功!", "提示", MessageBoxButtons.OK);
            }
            catch
            {
                MessageBox.Show("设置信息保存失败!", "提示", MessageBoxButtons.OK);
            }



Tag:C# 写入XML文件 读取XML文件
相关文章

发表评论: