Praveen
Praveen

Reputation: 71

How to copy xml file contents from one xml file to another xml file using Powershell

From below web.config file I want to copy the contents to another config file:
 <?xml version="1.0"?>     
     <configSections>...</configSections>   
    <system.webserver>...</system.webserver> 
        <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;"/>
                    <add key="key1" value ="value1"/>
                    <add key="key2" value ="value2"/>
                    <add key="key3" value ="value3"/>
                    <add key="key4" value ="value4"/>
                    .....<add key="key30" value ="value30"/>
                  </appSettings>
            </configuration>

Tags which are copied from above config file need to show as below Expected config file "web1.config" with following contents:

From above xml file I want to copy  only "ConnectionString,ConnectionString1,ConnectionString2" tags from <appSettings> parent tag as

<?xml version="1.0"?>
             <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>
            <settings>
            <add key="key1" value ="value1"/>
                    <add key="key2" value ="value2"/>
                    <add key="key3" value ="value3"/>
                    <add key="key4" value ="value4"/>
                    .....<add key="key30" value ="value30"/>
            </settings>

Upvotes: 0

Views: 954

Answers (2)

Praveen
Praveen

Reputation: 71

Reference config file:

<?xml version="1.0"?>
<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;"/>
            <add key="key1" value ="value1"/>
            <add key="key2" value ="value2"/>
            <add key="key3" value ="value3"/>
            <add key="key4" value ="value4"/>
            .....<add key="key5" value ="value30"/>
          </appSettings>
    </configuration>

Expected config file:

<?xml version="1.0"?>
     <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>
    <settings>
    <add key="key1" value ="value1"/>
            <add key="key2" value ="value2"/>
            <add key="key3" value ="value3"/>
            <add key="key4" value ="value4"/>
            .....<add key="key5" value ="value30"/>
    </settings>

Upvotes: 0

Martin Brandl
Martin Brandl

Reputation: 59001

The script retrieves the ConnectionString keys and values from the reference XML using a regular expression and adds it to the destination XML using System.Xml.XmlDocument.

$referenceXMLPath = 'c:\test1.xml'
$destinationXMLPath = 'c:\test2.xml'

$referenceContent = (gc $referenceXMLPath -Raw)
$destinationContent = [xml](gc $referenceXMLPath -Raw)

foreach ($connectionString in [regex]::Matches($referenceContent, '<add key="(ConnectionString[^"]*).*value="([^"]*)'))
{
    $key = $connectionString.Groups[1].Value
    $value = $connectionString.Groups[2].Value

    $child = $destinationContent.CreateElement("add")

    $keyAttribute = $destinationContent.CreateAttribute("key")
    $keyAttribute.Value = $key
    $child.Attributes.Append($keyAttribute)

    $valueAttribute = $destinationContent.CreateAttribute("value")
    $valueAttribute.Value = $value
    $child.Attributes.Append($valueAttribute)

    $destinationContent.configuration.appSettings.AppendChild($child)

}

$destinationContent.Save($destinationXMLPath)

Upvotes: 1

Related Questions