To update entries other than appsettings
, simply use XmlDocument
.
public static void UpdateAppConfig(string tagName, string attributeName, string value)
{
var doc = new XmlDocument();
doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
var tags = doc.GetElementsByTagName(tagName);
foreach (XmlNode item in tags)
{
var attribute = item.Attributes[attributeName];
if (!ReferenceEquals(null, attribute))
attribute.Value = value;
}
doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
This is how you call it:
Utility.UpdateAppConfig("endpoint", "address", "http://localhost:19092/NotificationSvc/Notification.svc");
Utility.UpdateAppConfig("network", "host", "abc.com.au");
This method can be improved to cater for appSettings values as well.