February 4, 2016

Change Web.config dynamically

We can update web.config dynamically in C#. Sometimes it will require to do some change in web.config at run time.
Using below approach we can update web.config at run time.

var xmlDoc = new XmlDocument();
xmlDoc.Load(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

string xmlContent = "XML Node";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlContent);

XmlNode newNode = doc.DocumentElement;
XmlNode impnode = xmlDoc.ImportNode(newNode, true);
 
xmlDoc.SelectSingleNode(
"//sitecore/pipelines").AppendChild(impnode); 
xmlDoc.Save(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
ConfigurationManager.RefreshSection("sitecore/pipelines");


Thanks
Enjoy Learning

 

No comments:

Post a Comment