/// <summary> /// Gets Settings from the XML settings.config /// </summary> /// <returns>Serializes settings from XML file into a nice list of objects</returns> public List<SettingsValue> GetSettings() { //A list to store our settings var settings = new List<SettingsValue>(); //Path to the settings.config file var configPath = HostingEnvironment.MapPath(ConfigPath); //Load settings.config XML file XmlDocument settingsXml = new XmlDocument(); settingsXml.Load(configPath); //Get all child nodes of <Analytics> node XmlNodeList analayticsNode = settingsXml.SelectNodes("//Analytics"); //Ensure we found the <Analytics> node in the config if (analayticsNode != null) { //Loop over child nodes inside <Analytics> node foreach (XmlNode settingNode in analayticsNode) { foreach (XmlNode setting in settingNode.ChildNodes) { //Go & populate our model from each item in the XML file var settingToAdd = new SettingsValue(); settingToAdd.Key = setting.Name; settingToAdd.Label = setting.Attributes["label"].Value; settingToAdd.Description = setting.Attributes["description"].Value; settingToAdd.Value = setting.InnerText; //Add the item to the list settings.Add(settingToAdd); } } } //Return the list return settings; }
public SettingsValue PostSetting(SettingsValue setting) { //Update the XML config file on disk //Path to the settings.config file var configPath = HostingEnvironment.MapPath(ConfigPath); //Load settings.config XML file XmlDocument settingsXml = new XmlDocument(); settingsXml.Load(configPath); //Get all child nodes of <Analytics> node XmlNode settingNode = settingsXml.SelectSingleNode("//Analytics/" + setting.Key); //Go & update the value if (settingNode != null) { settingNode.InnerText = setting.Value; } //Save the XML file back down to disk settingsXml.Save(configPath); //Return the same setting that passed in return setting; }
public SettingsValue GetSetting(string key) { //Path to the settings.config file var configPath = HostingEnvironment.MapPath(ConfigPath); //Load settings.config XML file XmlDocument settingsXml = new XmlDocument(); settingsXml.Load(configPath); //Get specific node XmlNode settingNode = settingsXml.SelectSingleNode("//Analytics/" + key); if (settingNode != null) { //Go & populate our model var setting = new SettingsValue(); setting.Key = settingNode.Name; setting.Label = settingNode.Attributes["label"].Value; setting.Description = settingNode.Attributes["description"].Value; setting.Value = settingNode.InnerText; return setting; } return null; }