【问题标题】:How to copy xml file contents from one xml file to another xml file using Powershell如何使用 Powershell 将 xml 文件内容从一个 xml 文件复制到另一个 xml 文件
【发布时间】:2015-03-30 09:29:26
【问题描述】:
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>

【问题讨论】:

    标签: xml powershell iis powershell-ise cmdlet


    【解决方案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)
    

    【讨论】:

    • 在我的场景中,目标文件没有。它应该在脚本中给出的指定路径中创建。需要将根标签从 web.config 复制到 web1.config。并且 appsettings 的顺序在 web1.config 文件中应与 web.config 文件保持一致。
    • appsettings 的顺序在 XML 中不相关。复制根标签是什么意思?您写了“从上面的 xml 文件中,我只想从 复制“ConnectionString、ConnectionString1、ConnectionString2”标签。您的意思是要将引用 xml 转换为新的 xml,还是只想将连接字符串从引用复制到目标?
    • 是的,复制 connectionstrings 的根标签和键,从 key1 到 key30 的值到一些根标签,如 key,值从 1 到 30 到代码中指定的目的地(目的地xml文件需要动态创建)
    • @Praveen 你能发布一个完整的样本(参考和预期结果)
    • 请检查。我已经发布了参考和预期结果
    【解决方案2】:

    参考配置文件:

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

    【讨论】:

    • 预期配置中没有根元素?
    • 也许只是将我的脚本作为参考并根据您的需要进行调整
    • 是的,预期的配置文件没有根元素
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多