2

From below xml snippet I want to remove only "ConnectionString" tag from <appSettings> parent tag:

     <configuration>  
        <appSettings>
            <add key="ConnectionString" value=" Data Source=192.168.1.19;Network Library=DBMSSOCN;Initial Catalog=Demo;Persist Security Info=True; User ID=sa;Password=powershell;Application Name=web;Connect Timeout=200; pooling='true'; Max Pool Size=200;"/>
            <add key="ConnectionString1" value=" Data Source=192.168.1.19;Network Library=DBMSSOCN;Initial Catalog=Demo;Persist Security Info=True; User ID=sa;Password=powershell;Application Name=web;Connect Timeout=200; pooling='true'; Max Pool Size=200;"/>
            <add key="ConnectionString2" value=" Data Source=192.168.1.19;Network Library=DBMSSOCN;Initial Catalog=Demo;Persist Security Info=True; User ID=sa;Password=powershell;Application Name=web;Connect Timeout=200; pooling='true'; Max Pool Size=200;"/>
          </appSettings>
    </configuration>

Please let me know how to do this using powershell?

2 Answers 2

3

Try this:

# Set file path
$File = '.\config.xml'

# Get file contents as XML
[xml]$xml = Get-Content $File

# Find node with key="ConnectionString"
$Remove = $xml.appSettings.configuration.appSettings.add |
                Where-Object {$_.Key -eq 'ConnectionString'}

# Remove this node from it's parent
$xml.appSettings.configuration.appSettings.RemoveChild($Remove) | Out-Null

# Save file
$xml.Save($File)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks its worked for me. After removing Connection strings from appSettings tag. Still I have to select 30+ tags from appSettings tag and need to copy the selected tags to another xml file. Please let me know how to do this? Tags are having key values like, <add key="PassWord" value="1234"/>
2

Removing is done by node's parent. First find the desired node and you'll get its parent via ParentNode property. Then via the parent, remove the child node via RemoveChild(). Like so,

[xml]$doc = cat 'path/to/xml'
$nodeToRemove = $doc.SelectSingleNode("//add[@key='ConnectionString']")
$parent = $nodeToRemove.ParentNode
$parent.RemoveChild($nodeToRemove)
$doc.Save([console]::out)

1 Comment

SelectSingleNode is not working in powershell 2.0? SoI have used like this $doc .configuration.appSettings.add | Where-Object {$_.Key -eq 'ConnectionString'}. While saving into file I am getting below error:Cannot convert value "System.Xml.XmlElement" to type "System.Xml.XmlDocument". Error: "Data at the root level is invalid. Line 1, position 1."At line:11 char:32 + $parent = [xml] $nodeToRemove. <<<< ParentNode + CategoryInfo : NotSpecified: (:) [], RuntimeException + FullyQualifiedErrorId : RuntimeException

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.