using System; using System.Xml.Linq; using System.Linq; using System.Configuration; using System.IO; using System.Collections.Generic; namespace AppConfigParser { public class XmlConfigurationParser : IAppConfigurationParser { public void Dispose() { } public void LoadFrom(string path) { XDocument document = XDocument.Load(path); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); foreach (XElement node in document.Descendants("parameter")) { bool isConnectionString = node.Attribute("connectionString")?.Value == "true"; string name = node.Attribute("name")?.Value; string value = node.Descendants("value").FirstOrDefault().Value; string description = node.Descendants("description").FirstOrDefault().Value; if (!isConnectionString) { ConfigurationManager.AppSettings[name] = value; if (config.AppSettings.Settings[name] != null) { config.AppSettings.Settings[name].Value = value; } else { config.AppSettings.Settings.Add(name, value); } } else { if (config.ConnectionStrings.ConnectionStrings[name] != null) { config.ConnectionStrings.ConnectionStrings[name].ConnectionString = value; } else { config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(name, value)); } } } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appConfig"); ConfigurationManager.RefreshSection("connectionStrings"); } public void SaveTo(string path, bool overwriteDefaultConfig = false) { if (File.Exists(path)) { XDocument document = XDocument.Load(path); List descendants = document.Descendants("parameter").ToList(); XElement documentConfiguration = document.Descendants("configuration").FirstOrDefault(); foreach (string key in ConfigurationManager.AppSettings.AllKeys) { XElement correspondingElement = descendants.Where(x => x.Attribute("name")?.Value == key).FirstOrDefault(); if (correspondingElement != null) { correspondingElement.Descendants("value").FirstOrDefault().Value = ConfigurationManager.AppSettings[key]; } else { XElement parameter = new XElement("parameter", new XAttribute("name", key)); XElement value = new XElement("value", new XText(ConfigurationManager.AppSettings[key])); XElement description = new XElement("description"); parameter.Add(value); parameter.Add(description); documentConfiguration.Add(parameter); } } foreach (ConnectionStringSettings key in ConfigurationManager.ConnectionStrings) { XElement correspondingElement = descendants.Where(x => x.Attribute("name")?.Value == key.Name).FirstOrDefault(); if (correspondingElement != null) { correspondingElement.Descendants("value").FirstOrDefault().Value = key.ConnectionString; } else { XElement parameter = new XElement("parameter", new XAttribute("name", key.Name), new XAttribute("connectionString", "true")); XElement value = new XElement("value", new XText(key.ConnectionString)); XElement description = new XElement("description"); parameter.Add(value); parameter.Add(description); documentConfiguration.Add(parameter); } } document.Save(path); } else { XDocument document = new XDocument(); XElement root = new XElement("configuration"); foreach (string key in ConfigurationManager.AppSettings.AllKeys) { XElement parameter = new XElement("parameter", new XAttribute("name", key)); XElement value = new XElement("value", new XText(ConfigurationManager.AppSettings[key])); XElement description = new XElement("description"); parameter.Add(value); parameter.Add(description); root.Add(parameter); } foreach (ConnectionStringSettings key in ConfigurationManager.ConnectionStrings) { XElement parameter = new XElement("parameter", new XAttribute("name", key.Name), new XAttribute("connectionString", "true")); XElement value = new XElement("value", new XText(key.ConnectionString)); XElement description = new XElement("description"); parameter.Add(value); parameter.Add(description); root.Add(parameter); } document.Add(root); document.Save(path); } if (overwriteDefaultConfig) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); foreach (string key in ConfigurationManager.AppSettings.AllKeys) { config.AppSettings.Settings[key].Value = ConfigurationManager.AppSettings[key].ToString(); } foreach (ConnectionStringSettings key in ConfigurationManager.ConnectionStrings) { config.ConnectionStrings.ConnectionStrings[key.Name].ConnectionString = key.ConnectionString; } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appConfig"); ConfigurationManager.RefreshSection("connectionStrings"); } } } }