winform对xml文件的操作


1.Xml文档如下
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<add key="IIDataBase" value="Server=192.168.10.12,2433;database=mang;user id=sa;password=2008sqlpwd;">
</add>
<add key="IIIDataBase" vaule="Server=192.168.10.22,6666;database=iii;user id=sa;password=nbPwd;">
</add>
<add key="RegisterDescription" value="用户根据系统提供的机器码经由算号器计算出注册号填入下框中进行产品注册。\r\n注:\r\n1.算号器由专人负责。\r\n2.如有其他问题联系管理员。">
</add>
<add key="RegisterCode" value="dsafds">
</add>
</configuration>

2.定义一个类用来操作xml文档
    public class Config
    {
        private XmlDocument xmlDoc; //引入命名空间:using System.Xml;
        private string filePath = Application.StartupPath + @"\help\config.xml"; //引入命名空间:using System.Windows.Forms;

        public Config()
        {
            xmlDoc = new XmlDocument();           
            xmlDoc.Load(filePath);
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        private string GetXmlValue(string strKey)
        {
            string strResult = "9999"; //找不到匹配的项;
            for (int i = 0; i < xmlDoc.DocumentElement.ChildNodes.Count; i++)
            {
                if (xmlDoc.DocumentElement.ChildNodes[i].Attributes[0].Value == strKey)
                {
                    strResult = xmlDoc.DocumentElement.ChildNodes[i].Attributes[1].Value;
                    break;
                }
            }
            return strResult;
        }
        /// <summary>
        /// 修改值
        /// </summary>
        /// <param name="strKey"></param>
        /// <param name="strValue"></param>
        /// <returns></returns>
        private string SetXmlValue(string strKey,string strValue)
        {
            string strResult = "9999"; //找不到匹配的项;
            for (int i = 0; i < xmlDoc.DocumentElement.ChildNodes.Count; i++)
            {
                if (xmlDoc.DocumentElement.ChildNodes[i].Attributes[0].Value == strKey)
                {
                    xmlDoc.DocumentElement.ChildNodes[i].Attributes[1].Value = strValue;
                    strResult = "0"; //修改成功
                    xmlDoc.Save(filePath); //修改完后记得保存o(∩_∩)o
                    break;
                }
            }
            return strResult;
        }
    }



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值