场景

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

https://blog.csdn.net/badao_liumang_qizhi/article/details/100532137

在上面已经对xml配置文件对节点能进行读取与写入之后 ,实现对节点元素的

添加与删除。

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

xml配置文件如下

<?xml version="1.0" encoding="utf-8" ?>
<configure>
    <!--y轴集合-->
    <yaxis>
        <!--第一条y轴-->
        <yaxi>
            <no>1</no>
            <title>温度</title>
            <color>black</color>
            <min>-1500</min>
            <max>1500</max>
        </yaxi>
        <!--第二条y轴-->
        <yaxi>
            <no>2</no>
            <title>电压</title>
            <color>black</color>
            <min>-1500</min>
            <max>1500</max>
        </yaxi>
    </yaxis>
   
</configure>

 

实现

添加节点

在工具类中新增方法

 public static void addnode()
        {
            //获取可执行文件的路径-即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");
                //读取节点数据
                if (configure != null)
                {
                    xmlnode yaxi = xml.createnode(xmlnodetype.element, "yaxi", null);
                    //创建no节点
                    xmlnode no = xml.createnode(xmlnodetype.element, "yaxi", null);
                    no.innertext = "3";
                    yaxi.appendchild(no);
                    //创建title节点
                    xmlnode title = xml.createnode(xmlnodetype.element, "title", null);
                    title.innertext = "badao";
                    yaxi.appendchild(title);
                    //创建color节点
                    xmlnode color = xml.createnode(xmlnodetype.element, "color", null);
                    color.innertext = "red";
                    yaxi.appendchild(color);
                    //创建min节点
                    xmlnode min = xml.createnode(xmlnodetype.element, "min", null);
                    min.innertext = "-1600";
                    yaxi.appendchild(min);
                    //创建max节点
                    xmlnode max = xml.createnode(xmlnodetype.element, "max", null);
                    max.innertext = "1600";
                    yaxi.appendchild(max);
                    //将yaxi追加到yaxis
                    configure.appendchild(yaxi);
                    xmlnodelist nodelist = xml.selectnodes("configure/yaxis/yaxi");
                    xml.save(path);
                    messagebox.show("nodelist[0]是:" + nodelist[0].childnodes[1].innertext);
                    messagebox.show("nodelist[1]是:" + nodelist[1].childnodes[1].innertext);
                    messagebox.show("nodelist[2]是:" + nodelist[2].childnodes[1].innertext);
                }
            }
            catch (exception ex)
            {
                console.writeline(ex.message);
            }
        }

 

注:

主要通过xmldocument.createnode来创建节点。

第一个参数是节点类型,element代表是节点。

第二个参数name属性。

第三个参数是命名空间,这里为null

以上效果就是完整的添加了一条y轴以及相关属性。

然后新建一个按钮,在点击事件中调用工具类中的方法。

private void simplebutton3_click(object sender, eventargs e)
        {
            configaccessutils.addnode();
        }

 

效果

添加之前

 

 

点击添加按钮后

 

 

添加之后

 

 

删除节点

工具类中新建方法

public static void removenode()
        {
            //获取可执行文件的路径-即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");
                //读取节点数据
                if (configure != null)
                {
                    xmlnodelist nodelist = xml.selectnodes("configure/yaxis/yaxi");
                    messagebox.show("删除之前count是:" + nodelist.count);
                    //将第三个节点删除
                    configure.removechild(configure.childnodes[2]);
                    nodelist = xml.selectnodes("configure/yaxis/yaxi");
                    xml.save(path);
                    messagebox.show("删除之后count是:" + nodelist.count);
                    
                }
            }
            catch (exception ex)
            {
                console.writeline(ex.message);
            }
        }

 

注:

是通过xmlnode.removechild(xmlnode)来实现删除子节点的。

selectnodes可以获得所有配置的节点。

效果

删除之前

 

 

删除之后