Change a specific key value in app.config file with another value.

Christian Palacios
Christian Palacios used Ask the Experts™
on
Hi there,

After I run a job, I need to replace this entire key in a app.config file, but I'm not sure exactly how to replace the entire key.  

<add key="ORMDBSessionFileOrConString" value="text to change" />

I need to be able to run a script (batch/VBscript/anything) that will look for this exact key entry and replace the value of it.  

Any ideas/suggestions?

Thanks in advance!
- Christian
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Use Powershell, and do it properly instead of a textual search and replace.
First, you'll need the "XPath"; you didn't post the complete XML, so you'll have to figure it out yourself.
It's basically  the node path down to the node you want to change.
Example:
<root>
	<config>
		<add key="ORMDBSessionFileOrConString" value="text to change" />
		<add key="WhateverElse" value="text to change" />
	</config>
</root>

Open in new window

To identify the node to change in the XML above, the XPath would be "root/config/add[@key='ORMDBSessionFileOrConString']".
The [@key=...] identifies the node to change by its attribute 'key', with the value 'ORMDBSessionFileOrConString'.
If the "add" node is the only one of that name in its parent node, you don't need the part in square brackets: "root/config/add" will do.
Note that node names, attributes names, and the attribute values in an XPath are CaSe SeNsItIvE.

Then it's easy:
$ConfigFile = 'C:\Whatever\app.config'
## XML Node names and Attributes are CaSe SeNsItIvE!
$XPath = "root/config/add[@key='ORMDBSessionFileOrConString']"
$Attribute = "value"
$NewValue = "The new text"

$xml = [xml](Get-Content -Path $ConfigFile)
$xml.SelectSingleNode($XPath).SetAttribute($Attribute, $NewValue)
$xml.Save($ConfigFile)

Open in new window

Christian PalaciosSenior IT Systems Administrator

Author

Commented:
Thanks for your reply!  Here is the exact first lines in the .config file to the keys I need to modify:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ORMDBSessionFileOrConString" value="data1" />
    <add key="TargetDBSessionFileOrConString" value="data2" />
..
..
  </appSettings>
</configuration>

So taking your example script, here's what I have:

##########################################################################
$ConfigFile = 'C:\Whatever\app.config'
## XML Node names and Attributes are CaSe SeNsItIvE!
$XPath = "configuration/appSettings/add[@key='ORMDBSessionFileOrConString']"
$Attribute = "value"
$NewValue = "The new text"

$xml = [xml](Get-Content -Path $ConfigFile)
$xml.SelectSingleNode($XPath).SetAttribute($Attribute, $NewValue)
$xml.Save($ConfigFile)
##########################################################################

As you can see above, I actually have two keys I need to update, one right after the other.  Do I need a separate PowerShell script for the second key or can I copy what's already in the script and run it twice, with different values for the $XPath, $NewValue?

Does the PowerShell script need anything else to run properly, or should this be it?

Thanks!
- Christian
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
No need for two scripts; you can just copy the relevant parts; saving the xml would obviously be only required once.
Or you can hard-code the paths; the variables were there mainly to separate the settings from the code, but it's totally up to you.
##########################################################################
$ConfigFile = 'C:\Whatever\app.config'
## XML Node names and Attributes are CaSe SeNsItIvE!
$xml = [xml](Get-Content -Path $ConfigFile)

$xml.SelectSingleNode("configuration/appSettings/add[@key='ORMDBSessionFileOrConString']").SetAttribute("value", "data1")
$xml.SelectSingleNode("configuration/appSettings/add[@key='TargetDBSessionFileOrConString']").SetAttribute("value", "data2")

$xml.Save($ConfigFile)
##########################################################################

Open in new window

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Christian PalaciosSenior IT Systems Administrator

Author

Commented:
Awesome, thanks very much!  I'll give it a shot and test it out.  I'll report back.

- Christian
Christian PalaciosSenior IT Systems Administrator

Author

Commented:
Works perfectly!  Thank you!
i have  a config file as below
<meeServices>
  <edmServers>
    <server name="EDMServer" description="EDMServer" username="" password="" integratedSecurity="true" />
  </edmServers>
  <rdmServers>
    <server name="EDMServer" description="EDMServer" username="" password="" integratedSecurity="true" />
  </rdmServers>
  <settings>
    <add name="ExposureSource" value="path" />
    <add name="DomainDatabase" value="Server=HeadNode;Initial Catalog=RMS_MEE_Data;Integrated Security=True;" />
    <add name="ResultsDatabase" value="Server=HeadNode;Initial Catalog=RMS_MEE_Results;Integrated Security=True;" />
    <add name="JobManagerService" value="url" />
    <add name="licensecode" value="code" />
    <add name="WhiteListedIPAddresses" value="*" />
  </settings>
</meeServices>

 in settings , i want to change the name which contains licensecode to newcode. how do i do this.

$ConfigFile = 'C:\Users\psman\Downloads\dev_qa.config'
## XML Node names and Attributes are CaSe SeNsItIvE!
$xml = [xml](Get-Content -Path $ConfigFile)

$xml.SelectSingleNode("meeservices/settings/add[@name='licensecode']").SetAttribute("name", "data1")

$xml.Save($ConfigFile)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial