Skip to content

Instantly share code, notes, and snippets.

@tncbbthositg
Created May 6, 2009 21:15
Show Gist options
  • Save tncbbthositg/107748 to your computer and use it in GitHub Desktop.
Save tncbbthositg/107748 to your computer and use it in GitHub Desktop.
ESG.Utilities.ConfigurationHelper.XmlConfigurationProvider
using System.Collections.Specialized;
using System.Configuration;
using System.Xml;
using System.IO;
using System;
namespace ESG.Utilities.ConfigurationHelper
{
public class XmlConfigurationProvider : IConfigurationProvider
{
private readonly NameValueCollection _appSettings;
private readonly ConnectionStringSettingsCollection _connectionStrings;
public XmlConfigurationProvider(string path)
{
_appSettings = new NameValueCollection();
_connectionStrings = new ConnectionStringSettingsCollection();
XmlDocument xd = new XmlDocument();
xd.LoadXml(File.ReadAllText(path));
XmlNodeList nl = xd.SelectNodes("//appSettings/*");
foreach (XmlNode n in nl)
AddNodes(n, (name, value) => _appSettings.Add(name, value));
nl = xd.SelectNodes("//connectionStrings/*");
foreach (XmlNode n in nl)
AddNodes(n, (name, value) => _connectionStrings.Add(new ConnectionStringSettings(name, value)));
}
private static void AddNodes(XmlNode node, Action<string, string> action)
{
AddNodes(node, string.Empty, action);
}
private static void AddNodes(XmlNode node, string path, Action<string, string> action)
{
if (node.ChildNodes.Count > 0)
foreach (XmlNode n in node.ChildNodes)
AddNodes(n, (!string.IsNullOrEmpty(path) ? path + "." : "") + node.Name, action);
else
action(path, node.InnerText.Trim());
}
public NameValueCollection AppSettings
{
get { return _appSettings; }
}
public ConnectionStringSettingsCollection ConnectionStrings
{
get { return _connectionStrings; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment