Skip to content

Instantly share code, notes, and snippets.

@mbrownnycnyc
Created March 28, 2013 13:48
Show Gist options
  • Save mbrownnycnyc/5263241 to your computer and use it in GitHub Desktop.
Save mbrownnycnyc/5263241 to your computer and use it in GitHub Desktop.
a c# function to read XML. I think I should be using XMLReader() instead, but this works fine.
public static Dictionary<string, string> ReadXML(string filename)
{
Dictionary<string, string> dcxml_config = new Dictionary<string, string>();
//thank you: http://www.mastercsharp.com/Article/69/working-with-xml-dom
FileStream docIn = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(docIn);
docIn.Close();
//read some of the appSettings from the XML file into a key/value store (dictionary)
XmlNodeList nodelist = xmldoc.GetElementsByTagName("appSettings");
foreach (XmlElement node in nodelist)
{
XmlNodeList childnodes = node.ChildNodes;
foreach (XmlElement cnode in childnodes)
{
XmlAttributeCollection fcn_attributes = cnode.Attributes;
//this is only proprietary in that the names of the attribute names are considered to be objects.
// we have to consider that there are only two elements, since we are adding them into a dictionary as key,value pairs.
string key = "";
string value = "";
foreach (XmlAttribute fcn_attribute in fcn_attributes)
{
if (fcn_attribute.Name == "key")
{
key = fcn_attribute.Value;
}
if (fcn_attribute.Name == "value")
{
value = fcn_attribute.Value;
}
}
dcxml_config.Add(key, value);
}
}
//done reading the config out of the XML
//maybe a check like we did for the input arguments here would be good...
// maybe via it's own function
return dcxml_config;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment