public class DefaultConfig : IConfig {
// keep a dictionary of values
private IDictionary m_keyValuePairs
// implement methods of IConfig using the above
// dictionary details left to you
// Constructor, where it reads your SimpleConfiguration
// XML node using the section handler above
public DefaultConfig()
{
// read the xml section for general config
// Section name: SimpleConfiguration
XmlNode xmlNode =
(XmlNode)System
.Configuration
.ConfigurationSettings
.GetConfig("SimpleConfiguration");
if(xmlNode != null)
{
m_keyValuePairs = createDictionary(m_genConfigXmlNode);
}
}
}
// Here is the createdictionary
private IDictionary createDictionary(XmlNode genConfigXmlNode)
{
IDictionary ht = new Hashtable();
if(genConfigXmlNode != null &&
genConfigXmlNode.ChildNodes != null &&
genConfigXmlNode.ChildNodes.Count > 0)
{
// walk through each node
// if it is a leaf node add it to the hash table
// if it is not continue the process
walkTheNode(ht,"",genConfigXmlNode);
}
return ht;
}
// Here is how you walk the nodes recursively
// to get your keys
private void walkTheNode(IDictionary ht, string parent, XmlNode node)
{
if(node != null)
{
if (node.NodeType == XmlNodeType.Comment)
{
return;
}
if (node.HasChildNodes == false)
{
if (node.NodeType == XmlNodeType.Text)
{
// no children
string leaf = node.Value;
ht.Add(parent.ToLower(),leaf);
// end of the recursive call
return;
}
else if (node.NodeType == XmlNodeType.CDATA)
{
XmlCDataSection cdataSection = (
System.Xml.XmlCDataSection)node;
string leaf = cdataSection.Data;
ht.Add(parent.ToLower(),leaf);
// end of the recursive call
return;
}
else
{
string key = parent + "/" + node.Name;
string val = "";
ht.Add(key.ToLower(), val);
return;
}
}
else
{
string newparent = parent + "/" + node.Name;
// has children
// walk all the children
for(int i=0;i<node.ChildNodes.Count;i++)
{
// recursive call
walkTheNode(ht,newparent,node.ChildNodes[i]);
}
}
}
}
|