0
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>

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

5 Comments

In my scenario destination file doesn't have.It should create in specified path given in script. Need to copy root tags from web.config to web1.config. and order of the appsettings should remain same in web1.config file as web.config file.
The order of the appsettings is not relevant in XML. What do you mean with copy root tags? You wrote 'From above xml file I want to copy only "ConnectionString,ConnectionString1,ConnectionString2" tags from <appSettings>'. Do you mean you want to transform the reference xml to a new xml or do you want to copy only the connection strings from the reference to a destination?
Yes, to copy root tag of <appsettings> connectionstrings </appsettings> and the key,values from key1 to key30 to some root tag like <application>key, values from 1 to 30</application> to specified destination in code (destination xml file need to create dynamically)
@Praveen Can you post a COMPLETE sample (reference and the excpected result)
Please check. I have posted reference and expected result
0

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>

3 Comments

there is no root element in the expected config?
Maybe just use my script as a reference and adapt it to your needs
Yes there no root element for expected config file

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.