Ejemplo n.º 1
0
        public void SetAppSetting(string name, string value)
        {
            // Ensure there's an <appSettings> node inside <configuration>
            // If not, create it.
            XmlNode settingsNode = config.SelectSingleNode("configuration/appSettings");

            if (settingsNode == null)
            {
                settingsNode = config.CreateNode(XmlNodeType.Element, "appSettings", "");
                config.SelectSingleNode("configuration").AppendChild(settingsNode);
            }

            XmlAttribute key = config.CreateAttribute("key");

            key.Value = name;

            XmlAttribute val = config.CreateAttribute("value");

            val.Value = value;

            bool found = false;

            foreach (XmlNode n in settingsNode.ChildNodes)
            {
                if (n.Name == "add" && n.Attributes.GetNamedItem("key").Value == name)
                {
                    n.Attributes.SetNamedItem(key);
                    n.Attributes.SetNamedItem(val);
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                XmlNode itemNode = config.CreateNode(XmlNodeType.Element, "add", "");
                settingsNode.AppendChild(itemNode);
                itemNode.Attributes.SetNamedItem(key);
                itemNode.Attributes.SetNamedItem(val);
            }
        }