NameValueSectionHandler.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // System.Configuration.NameValueSectionHandler.cs
  3. //
  4. // Author:
  5. // Christopher Podurgiel ([email protected])
  6. //
  7. // (C) Chris Podurgiel
  8. //
  9. using System;
  10. using System.Xml;
  11. using System.Collections.Specialized;
  12. namespace System.Configuration
  13. {
  14. /// <summary>
  15. /// Summary description for NameValueSectionHandler.
  16. /// </summary>
  17. public class NameValueSectionHandler
  18. {
  19. private static string keyName;
  20. private static string valueName;
  21. private static NameValueCollection settingsCollection;
  22. /// <summary>
  23. /// NameValueSectionHandler Constructor
  24. /// </summary>
  25. public NameValueSectionHandler()
  26. {
  27. //Set Default Values.
  28. keyName = "key";
  29. valueName = "value";
  30. settingsCollection = new NameValueCollection();
  31. }
  32. /// <summary>
  33. /// Creates a new configuration handler and adds the specified configuration object to the collection.
  34. /// </summary>
  35. /// <param name="parent">Composed from the configuration settings in a corresponding parent configuration section.</param>
  36. /// <param name="context">Provides access to the virtual path for which the configuration section handler computes configuration values. Normally this parameter is reserved and is null.</param>
  37. /// <param name="section">The XML node that contains the configuration information to be handled. section provides direct access to the XML contents of the configuration section.</param>
  38. /// <returns></returns>
  39. public object Create(object parent, object context, XmlNode section)
  40. {
  41. //FIXME: I'm not quite sure how to implement 'parent' or 'context'.
  42. //Get all of the ChildNodes in the XML section.
  43. XmlNodeList childNodeList = section.ChildNodes;
  44. //loop throught the ChildNodes
  45. for (int i=0; i < childNodeList.Count; i++)
  46. {
  47. XmlNode childNode = childNodeList[i];
  48. //if the name of this childNode is not 'add' then throw a ConfigurationException.
  49. if(childNode.Name != "add")
  50. {
  51. throw (new ConfigurationException("Unrecognized element"));
  52. }
  53. //Get the attributes for the childNode
  54. XmlAttributeCollection xmlAttributes = childNode.Attributes;
  55. //Get the key and value Attributes by their Name
  56. XmlAttribute keyAttribute = xmlAttributes[keyName];
  57. XmlAttribute valueAttribute = xmlAttributes[valueName];
  58. //Add this Key/Value Pair to the collection
  59. settingsCollection.Add(keyAttribute.Value, valueAttribute.Value);
  60. }
  61. //FIXME: Something is missing here. MS's version of this method returns a System.Configuration.ReadOnlyNameValueCollection type,
  62. //this class id not documented ANYWHERE. This method is curretly returning a NameValueCollection, but it should be ReadOnly.
  63. return settingsCollection;
  64. }
  65. /// <summary>
  66. /// Gets the name of the key in the key-value pair.
  67. /// </summary>
  68. protected virtual string KeyAttributeName
  69. {
  70. get
  71. {
  72. return keyName;
  73. }
  74. }
  75. /// <summary>
  76. /// Gets the value for the key in the key-value pair.
  77. /// </summary>
  78. protected virtual string ValueAttributeName
  79. {
  80. get
  81. {
  82. return valueName;
  83. }
  84. }
  85. }
  86. }