XML RemoveChild throws Exception

rated by 0 users
This post has 1 Reply | 2 Followers

Top 150 Contributor
Posts 13
ewood Posted: 05-09-2012 3:55 PM

I’m processing a web.config file using XML and trying to remove certain appsetting add key/values entires.  I use the following code below:

 

 

$config.configuration.appSettings.ChildNodes |

        where {

            $_.key -ne $null -and $commonAppSettings -contains $_.key

        } |

        foreach {

                                                $config.configuration.appSettings.RemoveChild($_.Node)

        }

    $config.Save($configPath)

    return $settings

}

 

The problem is when it attempt to execute the  RemoveChild(), it throws the following error:

Exception calling "RemoveChild" with "1" argument(s): "Object reference not set to an instance of an object."

At C:\ws\coreServices\BuildProcessTemplates\Deployment\CoreDeploy.ps1:98 char:49

+             $config.configuration.appSettings.RemoveChild <<<< ($_.Node)

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

 

How do I properly reference the childnode to remove it?

Top 10 Contributor
Posts 658
Idera Employee

Not having the whole script, it's a little hard to see what is going wrong but the error message is telling you that $config.configuration.appSettings object is not initialized.

Take a look at this posting and see it gives you some context to help solve your problem:

http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/1b440c30-485f-4136-b1e4-3daf5e200170

Page 1 of 1 (2 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.