May 25, 2006

Using DOM to parse an Xml Document

XmlDocument xDoc = new XmlDocument();
try
{
  // load the configuration file
  xDoc.Load(sFileName);

  // find the node of interest from the key
  XmlNode theNode = 
  xDoc.SelectSingleNode(@"/thingy/appSettings/add[@key = '" + key + "\']");

  // retrieve the nodes value if it exists
  if (theNode != null)
    return theNode.Attributes["value"].Value;
}

No comments: