次の方法で共有


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="https://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="https://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

Comments

  • Anonymous
    May 30, 2009
    After updating and deleting element from an XML config file, lets see how to add new elements at the

  • Anonymous
    October 28, 2014
    Nice article