Beispiel #1
0
        /// <summary>
        /// Updates a setting in the config file.
        /// </summary>
        /// <param name="configXml">The XML document contains the configuration.</param>
        /// <param name="setting">The setting to add.</param>
        /// <param name="addSettings"></param>
        private void UpdateConfigurationSetting(XmlDocument configXml, ConfigurationSetting setting, bool addSettings)
        {
            // Attempt to find the parent element
            XmlElement element = configXml.SelectSingleNode(setting.Path) as XmlElement;

            if (element != null)
            {
                FireMessage(TraceLevel.Verbose, "Element found ('{0}'), updating", setting.Path);
                string xpath = string.IsNullOrEmpty(setting.Filter) ?
                               setting.Name :
                               string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}[{1}]", setting.Name, setting.Filter);
                if (addSettings)
                {
                    // Otherwise see if there is an old element, if there is then overwrite it, otherwise
                    // a new element must be added
                    XmlNode oldElement = element.SelectSingleNode(xpath);
                    if (oldElement != null)
                    {
                        FireMessage(TraceLevel.Info,
                                    "Setting element '{0}' on element '{1}' to value '{2}'",
                                    setting.Name,
                                    setting.Path,
                                    setting.Value);
                        oldElement.InnerText = setting.Value;
                        element = oldElement as XmlElement;
                    }
                    else
                    {
                        FireMessage(TraceLevel.Info,
                                    "Adding new element '{0}' to element '{1}' with value '{2}'",
                                    setting.Name,
                                    setting.Path,
                                    setting.Value);
                        XmlElement newElement = configXml.CreateElement(setting.Name);
                        newElement.InnerText = setting.Value;
                        element.AppendChild(newElement);
                        element = newElement;
                    }

                    // Set the attributes
                    foreach (ConfigurationAttribute attribute in setting.Attributes ?? new ConfigurationAttribute[0])
                    {
                        FireMessage(TraceLevel.Info,
                                    "Setting attribute '{0}' on element '{1}' to value '{2}'",
                                    attribute.Name,
                                    setting.Name,
                                    attribute.Value);
                        element.SetAttribute(attribute.Name, attribute.Value);
                    }
                }
                else
                {
                    // See if the element is there
                    XmlNode oldElement = element.SelectSingleNode(xpath);
                    if (oldElement != null)
                    {
                        FireMessage(TraceLevel.Info,
                                    "Removing element '{0}' on element '{1}'",
                                    setting.Name,
                                    setting.Path);
                        oldElement.ParentNode.RemoveChild(oldElement);
                    }
                }
            }
            else
            {
                if (addSettings)
                {
                    FireMessage(TraceLevel.Warning, "Unable to find element '{0}'", setting.Path);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Updates a setting in the config file.
        /// </summary>
        /// <param name="configXml">The XML document contains the configuration.</param>
        /// <param name="setting">The setting to add.</param>
        /// <param name="addSettings"></param>
        private void UpdateConfigurationSetting(XmlDocument configXml, ConfigurationSetting setting, bool addSettings)
        {
            // Attempt to find the parent element
            XmlElement element = configXml.SelectSingleNode(setting.Path) as XmlElement;
            if (element != null)
            {
                FireMessage(TraceLevel.Verbose, "Element found ('{0}'), updating", setting.Path);
                string xpath = string.IsNullOrEmpty(setting.Filter) ? 
                    setting.Name : 
                    string.Format(System.Globalization.CultureInfo.CurrentCulture,"{0}[{1}]", setting.Name, setting.Filter);
                if (addSettings)
                {
                    // Otherwise see if there is an old element, if there is then overwrite it, otherwise
                    // a new element must be added
                    XmlNode oldElement = element.SelectSingleNode(xpath);
                    if (oldElement != null)
                    {
                        FireMessage(TraceLevel.Info,
                            "Setting element '{0}' on element '{1}' to value '{2}'",
                            setting.Name,
                            setting.Path,
                            setting.Value);
                        oldElement.InnerText = setting.Value;
                        element = oldElement as XmlElement;
                    }
                    else
                    {
                        FireMessage(TraceLevel.Info,
                            "Adding new element '{0}' to element '{1}' with value '{2}'",
                            setting.Name,
                            setting.Path,
                            setting.Value);
                        XmlElement newElement = configXml.CreateElement(setting.Name);
                        newElement.InnerText = setting.Value;
                        element.AppendChild(newElement);
                        element = newElement;
                    }

                    // Set the attributes
                    foreach (ConfigurationAttribute attribute in setting.Attributes ?? new ConfigurationAttribute[0])
                    {
                        FireMessage(TraceLevel.Info,
                            "Setting attribute '{0}' on element '{1}' to value '{2}'",
                            attribute.Name,
                            setting.Name,
                            attribute.Value);
                        element.SetAttribute(attribute.Name, attribute.Value);
                    }
                }
                else
                {
                    // See if the element is there
                    XmlNode oldElement = element.SelectSingleNode(xpath);
                    if (oldElement != null)
                    {
                        FireMessage(TraceLevel.Info,
                            "Removing element '{0}' on element '{1}'",
                            setting.Name,
                            setting.Path);
                        oldElement.ParentNode.RemoveChild(oldElement);
                    }
                }
            }
            else
            {
                if (addSettings) FireMessage(TraceLevel.Warning, "Unable to find element '{0}'", setting.Path);
            }
        }