Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Created July 15, 2015 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelovstanton/5e018a5b850a578cb710 to your computer and use it in GitHub Desktop.
Save angelovstanton/5e018a5b850a578cb710 to your computer and use it in GitHub Desktop.
public static void ChangeValueByKey(string key, string value, string attributeForChange, ConfigModificatorSettings configWriterSettings)
{
XmlDocument doc = ConfigModificator.LoadConfigDocument(configWriterSettings.ConfigPath);
XmlNode rootNode = doc.SelectSingleNode(configWriterSettings.RootNode);
if (rootNode == null)
{
throw new InvalidOperationException("the root node section not found in config file.");
}
try
{
XmlElement elem = (XmlElement)rootNode.SelectSingleNode(string.Format(configWriterSettings.NodeForEdit, key));
elem.SetAttribute(attributeForChange, value);
doc.Save(configWriterSettings.ConfigPath);
}
catch (Exception ex)
{
throw ex;
}
}
private static XmlDocument LoadConfigDocument(string configFilePath)
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(configFilePath);
return doc;
}
catch (System.IO.FileNotFoundException e)
{
throw new Exception("No configuration file found.", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment