NameValueSectionHandler
Provides name/value-pair configuration information from a configuration section.
using System.Xml;
namespace System.Configuration
{
public class NameValueSectionHandler : IConfigurationSectionHandler
{
private const string DefaultKeyAttribute = "key";
private const string DefaultValueAttribute = "value";
protected virtual string KeyAttributeName => "key";
protected virtual string ValueAttributeName => "value";
public object Create(object parent, object context, XmlNode section)
{
return CreateStatic(parent, section, KeyAttributeName, ValueAttributeName);
}
internal static object CreateStatic(object parent, XmlNode section)
{
return CreateStatic(parent, section, "key", "value");
}
internal static object CreateStatic(object parent, XmlNode section, string keyAttriuteName, string valueAttributeName)
{
ReadOnlyNameValueCollection readOnlyNameValueCollection = (parent != null) ? new ReadOnlyNameValueCollection((ReadOnlyNameValueCollection)parent) : new ReadOnlyNameValueCollection(StringComparer.OrdinalIgnoreCase);
HandlerBase.CheckForUnrecognizedAttributes(section);
foreach (XmlNode childNode in section.ChildNodes) {
if (!HandlerBase.IsIgnorableAlsoCheckForNonElement(childNode)) {
if (childNode.Name == "add") {
string name = HandlerBase.RemoveRequiredAttribute(childNode, keyAttriuteName);
string value = HandlerBase.RemoveRequiredAttribute(childNode, valueAttributeName, true);
HandlerBase.CheckForUnrecognizedAttributes(childNode);
readOnlyNameValueCollection[name] = value;
} else if (childNode.Name == "remove") {
string name2 = HandlerBase.RemoveRequiredAttribute(childNode, keyAttriuteName);
HandlerBase.CheckForUnrecognizedAttributes(childNode);
readOnlyNameValueCollection.Remove(name2);
} else if (childNode.Name.Equals("clear")) {
HandlerBase.CheckForUnrecognizedAttributes(childNode);
readOnlyNameValueCollection.Clear();
} else {
HandlerBase.ThrowUnrecognizedElement(childNode);
}
}
}
readOnlyNameValueCollection.SetReadOnly();
return readOnlyNameValueCollection;
}
}
}