NameValueFileSectionHandler.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // System.Configuration.NameValueFileSectionHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.Collections.Specialized;
  11. using System.IO;
  12. using System.Xml;
  13. namespace System.Configuration
  14. {
  15. public class NameValueFileSectionHandler : IConfigurationSectionHandler
  16. {
  17. public object Create (object parent, object configContext, XmlNode section)
  18. {
  19. XmlNode file = null;
  20. if (section.Attributes != null)
  21. file = section.Attributes.RemoveNamedItem ("file");
  22. NameValueCollection pairs = ConfigHelper.GetNameValueCollection (
  23. parent as NameValueCollection,
  24. section,
  25. "key",
  26. "value");
  27. if (file != null && file.Value == String.Empty) {
  28. if (!(file is IConfigXmlNode))
  29. return null;
  30. string fileName = ((IConfigXmlNode) file).Filename;
  31. string fullPath = Path.Combine (Path.GetDirectoryName (fileName), file.Value);
  32. if (!File.Exists (fullPath))
  33. return pairs;
  34. ConfigXmlDocument doc = new ConfigXmlDocument ();
  35. doc.Load (fullPath);
  36. if (doc.DocumentElement.Name != section.Name)
  37. throw new ConfigurationException ("Invalid root element", doc.DocumentElement);
  38. pairs = ConfigHelper.GetNameValueCollection (pairs, doc.DocumentElement,
  39. "key", "value");
  40. }
  41. return pairs;
  42. }
  43. }
  44. }