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>
从上面的配置文件复制的标签需要显示如下预期的配置文件“web1.config”,内容如下:
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>
答案 0 :(得分:1)
脚本使用正则表达式从参考XML中检索ConnectionString键和值,并使用 System.Xml.XmlDocument 将其添加到目标XML。
$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)
答案 1 :(得分:0)
参考配置文件:
<?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>
预期的配置文件:
<?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>