Welcome to MSDN Blogs Sign in | Join | Help

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

This post talks about removing an existing XML element from the config file. Consider a scenario where we have the following web.config file saved at location C:\MyApplication

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="system.web">
      <section name="myConfigSection”/>
    </sectionGroup>
  </configSections>
  <system.webServer>
    <modules>
      <add name="moduleToRemove”/>
      <add name="moduleToKeep”/>
    </modules>
  </system.webServer>  
  <system.web>
    <customErrors mode="On" defaultRedirect="Error.htm">
      <error statusCode="404" redirect="http://myAppDevWeb/404.aspx"/>
    </customErrors>
  </system.web>
  <appSettings>
    <add key="DBServer" value="myAppDevDB"/>
  </appSettings>
</configuration>

The requirement is to remove the <configSections> and the module “moduleToRemove” . These sections are  marked in Red.

The resultant file should look like this

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

 

Lets have a look at the Powershell script we will be using. We will name this script RemoveElement.ps1 and save it in C:\Scripts

# Declaring the parameter to be passed while executing the Powershell script

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

$xml = [xml](get-content $webConfigPath)                                                      # Create XML object and open the web.config file

$nodeToRemove= $xml.configuration.SelectSingleNode("configSections");           # Selecting the Element to be removed
$xml.configuration.RemoveChild($nodeToRemove);                                          # Remove the Element

foreach( $item in  $xml.configuration."system.webServer".modules.add )             # Traverse through all modules
{
        if( $item.name -eq "moduleToRemove" )                                                 # Checking if the current module is to be removed
        {
              $xml.configuration."system.webServer".modules.RemoveChild($item);   # Remove the desired module when found
        }
}

 

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

 

Run the script:

c:\>Powershell

PS C:\> cd scripts

PS C:\scripts> .\RemoveElement.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

Published Friday, May 29, 2009 11:03 AM by ssehgal

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# 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

Saturday, May 30, 2009 10:15 AM by The Deployment guy

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker