winform中对XML文件的读,修改操作

// 在配置文件app.config中添加字段(在<configuration>节下写)

A)      声明一个节

 <configSections>

    <section name="ScreenLockInit" type="System.Configuration.NameValueSectionHandler" />

 </configSections>

 

B)      配置节中的值

<ScreenLockInit>

   <add key="DISENABLE_CTRL_ESC" value="false"/>

</ScreenLockInit>

 

//读方法

 1//读取XML文件
 2/// 取配置文件中的键值
 3/// <param name="index">配置文件中的索引名称  可视为上例中的ScreenLockInit值 </param>
 4/// <param name="key">KEY值  可视为上例中的DISENABLE_CTRL_ESC 值</param>
 5/// <returns>返回读取配置文件的值, 可视为上例中的值fasle</returns>

 6public static String getCustomValue(String index, String key)
 7{
 8NameValueCollection nvColl = (NameValueCollection)ConfigurationSettings.GetConfig(index);
 9String value = nvColl[key];
10return value;
11}

12
13
14//另一种方法:
15XmlNode tableNode = xmlTable.SelectSingleNode("configuration/custom/table[@name='" + tableName + "']");           //取出表节点
16XmlNode fieldNode = tableNode.SelectSingleNode("field[@name='" + fieldName + "']");                               //取出字段节点
17 XmlElement fieldElement = (XmlElement)fieldNode;                                                                //类型转换
18Hashtable has = new Hashtable();
19if (fieldElement != null)          //如果有对应的信息
20{
21has.Add(P_NAME, fieldElement.GetAttribute(P_NAME));//获得属性值
22has.Add(P_TEXT, fieldElement.GetAttribute(P_TEXT)); //获得属性值
23has.Add(P_ISFIELD, fieldElement.GetAttribute(P_ISFIELD)); //获得属性值
24}

25
26

 

 //修改方法


 1//修改XML文件(程序运行后会在bin目录下生成一个和exe文件同//名的配置文件,如:项目名为abc,则会生成abc.exe.config, //实际上修改的配置文件是abc.exe.config,而不是app.config)
 2 XmlDocument xmlDoc = new XmlDocument();
 3xmlDoc.Load(Application.ExecutablePath + ".config");//加载配置文件,同名.exe.config配置文件
 4XmlElement pwdElement = (XmlElement)xmlDoc.SelectSingleNode("configuration/ScreenLockInit/add[@key='UNLOCK_PASSWORD']");           //取出表节点
 5if (pwdElement != null)
 6{
 7pwdElement.SetAttribute("value","true");  //设置属性值
 8xmlDoc.Save(Application.ExecutablePath + ".config");//保存配置
 9Properties.Settings.Default.Reload();//刷新
10}

11

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值