Automating config file changes : Part 4 – Adding a new element to an XML file

After updating and deleting element from an XML config file, lets see how to add new elements at the desired position in an XML file.

We will again take the same web.config file , which we took in the first example and save it at C:\MyApplication

<?xml version="1.0"?> <configuration>   <system.web>     <customErrors mode="On" defaultRedirect="Error.htm">       <error statusCode="404" redirect="https://myAppDevWeb/404.aspx"/>     </customErrors>   </system.web>   <appSettings>     <add key="DBServer" value="myAppDevDB"/>   </appSettings> </configuration>

 

We want to add a new element <add key="ReportingServer" value="myAppDevRpt"/> under the appsettings section if it does not exist. If it exists we will set the value as myAppDevRpt

The resultant file should look like this

<?xml version="1.0"?> <configuration>   <system.web>     <customErrors mode="On" defaultRedirect="Error.htm">       <error statusCode="404" redirect="https://myAppDevWeb/404.aspx"/>     </customErrors>   </system.web>   <appSettings>     <add key="DBServer" value="myAppDevDB"/>     <add key=”ReportingServer" value="myAppDevRpt"/>     </appSettings> </configuration>

 

Lets have a look at the script.  We will name this script addElement.ps1 and store it in C:\Scripts

# Parameter declaration which will be passed to the script during execution

Param (     $webConfigPath      #Path to the web.config file. Make sure file is not read-only ) $RptKeyFound=0;

$xml = [xml](get-content $webConfigPath);              # Create the XML Object and open the web.config file $root = $xml.get_DocumentElement();                     # Get the root element of the file

foreach( $item in $root.appSettings.add)                  # loop through the child items in appsettings {   if($item.key –eq “ReportingServer”)                       # If the desired element already exists     {       $item.value = “myAppDevRpt”;                          # Update the value attribute       $RptKeyFound=1;                                             # Set the found flag     } }

if($RptKeyFound -eq 0)                                                   # If the desired element does not exist {     $newEl=$xml.CreateElement("add");                               # Create a new Element     $nameAtt1=$xml.CreateAttribute("key");                         # Create a new attribute “key”     $nameAtt1.psbase.value="ReportingServer";                    # Set the value of “key” attribute     $newEl.SetAttributeNode($nameAtt1);                              # Attach the “key” attribute     $nameAtt2=$xml.CreateAttribute("value");                       # Create “value” attribute      $nameAtt2.psbase.value="myAppDevRpt";                       # Set the value of “value” attribute     $newEl.SetAttributeNode($nameAtt2);                               # Attach the “value” attribute     $xml.configuration["appSettings"].AppendChild($newEl);    # Add the newly created element to the right position

}

$xml.Save($webConfigPath)                                                # Save the web.config file

 

Run the script

Open Command Prompt

c:\>Powershell

PS C:\> cd scripts

PS C:\scripts> .\addElement.ps1 “C:\MyApplication\web.config”

 

 

Related Posts:

Automating config file changes : Part 1 – Installing Microsoft Windows Powershell

Automating config file changes : Part 2 – modifying already existing config keys

Automating config file changes : Part 3 – Deleting an element from an XML files