/// <summary> /// Creates a <see cref="NameValueSettings"/> from an xml element of a config file. /// <para> /// NOTE: The settings collection is case sensitive. /// </para> /// </summary> /// <param name="section">The XElement that contains the configuration information from the configuration file. Provides direct access to the XML contents of the configuration section</param> /// <param name="cryptoProvider">An <see cref="ICryptoProvider"/> instance that can be used to decrypt settings.</param> /// <param name="decryptAll">If <see langword="true" /> all settings will be decrypted. This is ignored since the <see cref="NameValueSettings"/> handles this by itself.</param> /// <returns>A <see cref="NameValueSettings"/> instance containing the configuration data, or empty if section is null or in fact empty.</returns> public object ReadSection(XElement section, ICryptoProvider cryptoProvider, bool decryptAll) { Ensure.ArgumentNotNull(section, "section"); _errors = null; //Create a case sensitive collection var data = new NameValueSettings(); if (section != null) { var settings = from node in section.Elements(ConfigElement.KeyValueSettingNode) let key = node.Attribute(ConfigElement.SettingKeyAttribute) let val = node.Attribute(ConfigElement.SettingValueAttribute) where key != null && val != null select new { Key = key.Value, Value = val.Value, Comment = node.PreviousNode as XComment }; foreach (var node in settings) { string val = node.Value; bool encrypted = false; if (cryptoProvider != null && cryptoProvider.IsEncrypted(val)) { try { val = cryptoProvider.Decrypt(val); encrypted = true; } catch { if (_errors == null) { _errors = new List<ConfigError>(); } _errors.Add( new ConfigError( ConfigErrorCode.InvalidConfigValue, string.Format("Reading value of setting {0} failed, value appears to be encrypted, but decryption failed.", node.Key))); continue; } } data.SetSetting(node.Key, val, encrypted); if (node.Comment != null) { data.SetDescription(node.Key, node.Comment.Value); } } } return data; }