首页 > 代码库 > winform中key读取修改

winform中key读取修改

根据key name的名称读取value-----读取使用ConfigurationManager.AppSettings读取容易没读取到根目录中的key

public string GetXml(string name)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("app.config");
            XmlElement element = doc.SelectSingleNode("//add[@key=‘" + name + "‘]") as XmlElement;

            return  element.GetAttributeNode("value").Value;
        }

 

根据key name名称修改value值

       public void UpdateXml(string name, string value)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("app.config");
            XmlElement element = doc.SelectSingleNode("//add[@key=‘" + name + "‘]") as XmlElement;
            if (element != null)
            {
                XmlAttribute a = element.GetAttributeNode("value");
                if (a != null)
                {
                    a.Value = value;// 按实际需要来写
                    doc.Save("app.config");
                }
            }
        }

 

winform中key读取修改