当前位置: 移动技术网 > IT编程>开发语言>c# > Winform中自定义xml配置文件后对节点进行读取与写入

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

2019年09月04日  | 移动技术网IT编程  | 我要评论
场景 Winform中自定义xml配置文件,并配置获取文件路径: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648 上面已经实现自定义配置文件的配置和读取的基础上,继续对配置文件进行读取与写入。 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>

 

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

大量编程视频教程:
 

实现

配置文件读取

添加一个工具类的方法

 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目录下的配置文件。

 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网