Winform中自定义xml配置文件后对节点进行读取与写入

场景

Winform中自定义xml配置文件,并配置获取文件路径:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648

上面已经实现自定义配置文件的配置和读取的基础上,继续对配置文件进行读取与写入。

xml配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<Configure>
    <!--Y轴数量 默认是1-->
    <yConut>1</yConut>
    <!--Y轴集合-->
    <YAxis>
        <!--第一条Y轴-->
        <YAxi>
            <num>1</num>
            <title>温度</title>
            <color>black</color>
            <min>-1500</min>
            <max>1500</max>
        </YAxi>
        <!--第二条Y轴-->
        <Yaxi>
            <num>2</num>
            <title>电压</title>
            <color>black</color>
            <min>-1500</min>
            <max>1500</max>
        </Yaxi>
    </YAxis>
  
</Configure>

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

大量编程视频教程:https://space.bilibili.com/164396311
 

实现

配置文件读取

添加一个工具类的方法

 public static void readConfig()
        {
            //获取可执行文件的路径-即bin目录下的debug或者release目录
            string context = System.Windows.Forms.Application.StartupPath;
            string path = String.Concat(context,@"\config\YAxisSet.xml");
            XmlDocument xml = new XmlDocument();
            //打开一个xml
            try
            {
                xml.Load(path);
                //选择匹配 XPath 表达式的第一个 XmlNode
                XmlNode Configure = xml.SelectSingleNode("Configure/YAxis/YAxi");
                //读取节点数据
                if (Configure !=null)
                {
                    string portName = Configure.SelectSingleNode("title").InnerText;
                    MessageBox.Show("第一个节点名是:" + portName);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
         }

然后添加一个按钮,在按钮的点击事件中调用此方法

 private void simpleButton1_Click(object sender, EventArgs e)
        {
            ConfigAccessUtils.readConfig();
        }

效果

 

写入配置文件

同样在工具类中新增方法

 public static void writeConfig()
        {
            //获取可执行文件的路径
            string context = System.Windows.Forms.Application.StartupPath;
            string path = String.Concat(context, @"\config\YAxisSet.xml");
            XmlDocument xml = new XmlDocument();
            //打开一个xml
            try
            {
                xml.Load(path);
                //选择匹配 XPath 表达式的第一个 XmlNode
                XmlNode Configure = xml.SelectSingleNode("Configure/YAxis/YAxi");
                //读取节点数据
                if (Configure != null)
                {
                    string portName = Configure.SelectSingleNode("title").InnerText;
                    MessageBox.Show("写入之前节点名是:" + portName);
                }
                //写入节点数据
                Configure.SelectSingleNode("title").InnerText = "霸道";
                string afterWrite = Configure.SelectSingleNode("title").InnerText;
                xml.Save(path);
                MessageBox.Show("写入之后节点名是:" + afterWrite);
              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

效果

写入之前

 

写入之后

 

注:

进行修改配置文件的内容,真正被修改的是bin下的debug目录下的配置文件。

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值