Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The way that I've done this in the past is use a custom configuration section and implement an <code>IConfigurationSectionHandler</code> for the section. You still have to do all of the parsing inside the configuration section handler, but it makes it easy to get the information in the configuration section in the form you want it in your application. There is a <a href="http://msdn.microsoft.com/en-us/library/ms228056.aspx" rel="noreferrer">How To article</a> on MSDN that goes through the process of creating such a handler.</p> <p>Below is an example of one that I've used:</p> <p>usage:</p> <pre><code>Dictionary&lt;string,AdministrativeRole&gt; roles = ConfigurationManager.GetSection("roles") as Dictionary&lt;string,AdministrativeRole&gt;; </code></pre> <p>web.config:</p> <pre><code>&lt;configuration&gt; &lt;configSections&gt; &lt;section name="roles" type="com.example.RolesDefinitionHandler, com.example" /&gt; &lt;/configSections&gt; &lt;roles&gt; &lt;role name="Administrator" group="domain\group-name" /&gt; ... &lt;/roles&gt; &lt;/configuration&gt; </code></pre> <p>code:</p> <pre><code>public class RolesDefinitionHandler : IConfigurationSectionHandler { private static readonly string ROLE_SECTION_NAME = "role"; private static readonly string ROLE_SECTION_NAME_ATTRIBUTE = "name"; private static readonly string ROLE_SECTION_GROUP_ATTRIBUTE = "group"; private Dictionary&lt;string, AdministrativeRole&gt; ReadConfiguration( XmlNode section ) { Dictionary&lt;string, AdministrativeRole&gt; roles = new Dictionary&lt;string, AdministrativeRole&gt;(); foreach (XmlNode node in section.ChildNodes) { if (node.Name.Equals( ROLE_SECTION_NAME, StringComparison.InvariantCultureIgnoreCase )) { string name = (node.Attributes[ROLE_SECTION_NAME_ATTRIBUTE] != null) ? node.Attributes[ROLE_SECTION_NAME_ATTRIBUTE].Value.ToLower() : null; if (string.IsNullOrEmpty( name )) { throw new ConfigurationErrorsException( "missing required attribute " + ROLE_SECTION_NAME_ATTRIBUTE ); } string group = (node.Attributes[ROLE_SECTION_GROUP_ATTRIBUTE] != null) ? node.Attributes[ROLE_SECTION_GROUP_ATTRIBUTE].Value.ToLower() : null; if (string.IsNullOrEmpty( group )) { throw new ConfigurationErrorsException( "missing required attribute " + ROLE_SECTION_GROUP_ATTRIBUTE ); } if (roles.ContainsKey( name )) { throw new ConfigurationErrorsException( "duplicate " + ROLE_SECTION_NAME + " for " + name ); } roles.Add( name, new AdministrativeRole( name, group ) ); } else { throw new ConfigurationErrorsException( "illegal node " + node.Name ); } } return roles; } #region IConfigurationSectionHandler Members public object Create( object parent, object configContext, System.Xml.XmlNode section ) { return ReadConfiguration( section ); } #endregion } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload