1 //------------------------------------------------------------------------------
2 // <copyright file="DictionarySectionHandler.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace System.Configuration {
8 using System.Collections;
9 using System.Collections.Specialized;
11 using System.Globalization;
14 /// Simple dictionary config factory
15 /// config is a dictionary mapping key->value
17 /// <add key="name" value="text"> sets key=text
18 /// <remove key="name"> removes the definition of key
19 /// <clear> removes all definitions
22 public class DictionarySectionHandler : IConfigurationSectionHandler {
25 /// Given a partially composed config object (possibly null)
26 /// and some input from the config system, return a
27 /// further partially composed config object
29 public virtual object Create(Object parent, Object context, XmlNode section) {
32 // start res off as a shallow clone of the parent
35 res = new Hashtable(StringComparer.OrdinalIgnoreCase);
37 res = (Hashtable)((Hashtable)parent).Clone();
41 HandlerBase.CheckForUnrecognizedAttributes(section);
43 foreach (XmlNode child in section.ChildNodes) {
45 // skip whitespace and comments, throws if non-element
46 if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child))
49 // handle <add>, <remove>, <clear> tags
50 if (child.Name == "add") {
51 HandlerBase.CheckForChildNodes(child);
52 String key = HandlerBase.RemoveRequiredAttribute(child, KeyAttributeName);
55 value = HandlerBase.RemoveRequiredAttribute(child, ValueAttributeName);
57 value = HandlerBase.RemoveAttribute(child, ValueAttributeName);
58 HandlerBase.CheckForUnrecognizedAttributes(child);
65 else if (child.Name == "remove") {
66 HandlerBase.CheckForChildNodes(child);
67 String key = HandlerBase.RemoveRequiredAttribute(child, KeyAttributeName);
68 HandlerBase.CheckForUnrecognizedAttributes(child);
72 else if (child.Name.Equals("clear")) {
73 HandlerBase.CheckForChildNodes(child);
74 HandlerBase.CheckForUnrecognizedAttributes(child);
78 HandlerBase.ThrowUnrecognizedElement(child);
86 /// Make the name of the key attribute configurable by derived classes.
88 protected virtual string KeyAttributeName {
93 /// Make the name of the value attribute configurable by derived classes.
95 protected virtual string ValueAttributeName {
96 get { return "value";}
100 internal virtual bool ValueRequired {
101 get { return false; }