Xml based configuration
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;
namespace Open_MUD.Kernel.Utils
{
class Config
{
private string CONFIG_FOLDER = "\\Config";
private string END_CONFIG_FILE = ".config.xml";
private static Config __conf=null;
private static Hashtable configs;
public static Config getInstance()
{
if (__conf == null)
__conf = new Config();
return __conf;
}
private Config()
{
LoadXML();
}
private void LoadXML()
{
configs = new Hashtable();
if (Directory.Exists(Directory.GetCurrentDirectory() + CONFIG_FOLDER))
{
DirectoryInfo dconfig = new DirectoryInfo(Directory.GetCurrentDirectory() + CONFIG_FOLDER);
foreach (FileInfo file in dconfig.GetFiles())
{
String key = file.Name;
if (key.EndsWith(END_CONFIG_FILE))
{
key = key.Substring(0, key.Length - END_CONFIG_FILE.Length);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(file.FullName);
configs.Add(key, xmldoc );
}
}
}
}
public int getIntValue(String xml, String key)
{
if (configs.ContainsKey(xml))
{
XmlNode nodo = (configs[xml] as XmlDocument).SelectSingleNode("/config/"+key);
if (nodo != null)
return Int32.Parse(nodo.InnerText);
}
throw new MudException("Fallo al recuperar " + key + " de " + xml);
}
internal string getStringValue(String xml, String key)
{
if (configs.ContainsKey(xml))
{
XmlNode nodo = (configs[xml] as XmlDocument).SelectSingleNode("/config/" + key);
if (nodo != null)
return nodo.InnerText;
}
throw new MudException("Fallo al recuperar " + key + " de " + xml);
}
}
}
Related examples in the same category