C#编程实现动态改变配置文件信息的方法
作者:aparche 时间:2022-05-01 15:09:25
本文实例讲述了C#编程实现动态改变配置文件信息的方法。分享给大家供大家参考,具体如下:
配置文件实际上就是一个XML文件,所以我们可以使用XmlDocument来进行操作。
代码如下:
static void Main(string[] args)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("../../App.config");//加载xml文件
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");//获取指定的xml子节点
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='type']");//获取子节点中指定的子节点
//如果能获取到节点,就修改节点的value值
if (xElem1 != null)
{
xElem1.SetAttribute("value", "driver");//给节点中的value属性赋值(修改操作)
}
//如果不能获取到节点,就创建节点
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", "type");
xElem2.SetAttribute("value","teacher");
xNode.AppendChild(xElem2);
}
xDoc.Save("../../App.config");//保存xml文档
Console.WriteLine("保存成功!");
}
希望本文所述对大家C#程序设计有所帮助。
标签:C#,动态改变,配置文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring AOP实现功能权限校验功能的示例代码
2021-10-18 21:32:29
Java 自定义Spring框架以及Spring框架的基本使用
2021-05-29 19:35:57
![](https://img.aspxhome.com/file/2023/7/62447_0s.png)
C#计算程序执行过程花费时间的方法
2022-08-07 08:44:53
Android Handler主线程和一般线程通信的应用分析
2022-10-29 14:08:13
一文带你搞懂Redis分布式锁
2021-09-26 12:56:14
![](https://img.aspxhome.com/file/2023/0/60920_0s.png)
java LeetCode刷题稍有难度的贪心构造算法
2022-01-06 20:56:21
![](https://img.aspxhome.com/file/2023/9/79209_0s.png)
Eclipse转Itellij IDEA导入Git/svn本地项目的详细步骤
2021-11-06 12:57:10
![](https://img.aspxhome.com/file/2023/1/103821_0s.png)
SpringBoot创建WebService方法详解
2022-02-08 10:29:31
![](https://img.aspxhome.com/file/2023/1/79821_0s.png)
Java中Lambda表达式的使用详解
2022-01-02 04:52:16
Springboot如何通过yml配置文件为静态成员变量赋值
2021-08-03 12:03:17
![](https://img.aspxhome.com/file/2023/1/78391_0s.png)
Windows 10上JDK环境安装配置图文教程
2023-05-31 19:38:03
![](https://img.aspxhome.com/file/2023/7/86737_0s.png)
C#实现虚拟键盘的实例详解
2021-09-21 08:59:47
![](https://img.aspxhome.com/file/2023/9/79079_0s.png)
Android实现复制Assets文件到SD卡
2022-03-21 03:36:53
![](https://img.aspxhome.com/file/2023/6/139186_0s.jpg)
java 二叉查找树实例代码
2022-07-23 22:54:28
Java实现求解一元n次多项式的方法示例
2022-08-24 09:03:33
![](https://img.aspxhome.com/file/2023/0/64790_0s.png)
c# 中文转拼音without CJK
2023-06-08 23:59:31
java Map接口子类HashMap遍历与LinkedHashMap详解
2021-11-18 04:43:38
![](https://img.aspxhome.com/file/2023/1/78341_0s.jpg)
IDEA中使用Typora编辑md文件的方法
2022-11-28 09:50:47
![](https://img.aspxhome.com/file/2023/5/132365_0s.png)
Unity使用LineRender实现签名效果
2023-08-02 20:40:00
![](https://img.aspxhome.com/file/2023/4/119544_0s.jpg)
SpringBoot整合Web开发之文件上传与@ControllerAdvice
2021-09-29 04:43:55
![](https://img.aspxhome.com/file/2023/5/119165_0s.png)