Winform中对自定义xml配置文件进行Xml节点的添加与删除 场景 实现
Winform中自定义xml配置文件后对节点进行读取与写入:
https://blog.****.net/BADAO_LIUMANG_QIZHI/article/details/100532137
在上面已经对xml配置文件对节点能进行读取与写入之后 ,实现对节点元素的
添加与删除。
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
大量编程视频教程:https://space.bilibili.com/164396311
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, @"configYAxisSet.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, @"configYAxisSet.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可以获得所有配置的节点。
效果
删除之前
删除之后