每个部分组合XML文件和“标题”

时间:2014-01-28 18:49:49

标签: xml xslt powershell

我需要组合几个xml文件并告诉xml哪个部分属于每个文件。以下是此类文件的两个示例。

d:\ pathtoxml \ 1.XML

<?xml version="1.0"?>
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    \Windows\Microsoft.Net\Framework\v2.x\Config 
-->
<configuration>
  <configSections>
    <section name="name" type="theres stuff here" />
  </configSections>
  <appSettings>
    <add key="key1" value="valueOfKey1" />
  </appSettings>
</configuration>

d:\ pathtoxml \ 2.XML

<?xml version="1.0"?>
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    \Windows\Microsoft.Net\Framework\v2.x\Config 
-->
<configuration>
  <appSettings>
    <add key="key1" value="valueofkey1" />
  </appSettings>
  <connectionStrings>
    <add name="connectionstring1" connectionString="connectionstringstuff" />
  </connectionStrings>
</configuration>

结束文件需要看起来像这样

<WebConfigs>
  <path = 'D:\pathtoxml\1.xml'>
    <!-- 
      Note: As an alternative to hand editing this file you can use the 
      web admin tool to configure settings for your application. Use
      the Website->Asp.Net Configuration option in Visual Studio.
      A full list of settings and comments can be found in 
      machine.config.comments usually located in 
      \Windows\Microsoft.Net\Framework\v2.x\Config 
    -->
    <configuration>
      <configSections>
        <section name="name" type="theres stuff here" />
      </configSections>
      <appSettings>
        <add key="key1" value="valueOfKey1" />
      </appSettings>
    </configuration>
  </path>
  <path = 'D:\pathtoxml\2.xml'>
    <!-- 
      Note: As an alternative to hand editing this file you can use the 
      web admin tool to configure settings for your application. Use
      the Website->Asp.Net Configuration option in Visual Studio.
      A full list of settings and comments can be found in 
      machine.config.comments usually located in 
      \Windows\Microsoft.Net\Framework\v2.x\Config 
    -->
    <configuration>
      <appSettings>
        <add key="key1" value="valueofkey1" />
      </appSettings>
      <connectionStrings>
        <add name="connectionstring1" connectionString="connectionstringstuff" />
      </connectionStrings>
    </configuration>
  </path>
</WebConfigs>

我当前的代码看起来像这样但是我得到了各种各样的XML错误

$path1 = 'D:\pathtoxml\1.xml'
$path2 = 'D:\pathtoxml\2.xml'
$exportPath = 'D:\exportPath\combined.xml'

$finalXml = "<path = '" + $path1 + "'>"
[xml]$xml = get-content $path1
$finalXml += $xml.InnerXml
$finalXml += "</path>"
$finalXml += "<path = '" + $path1 + "'>"
[xml]$xml = get-content $path2
$finalXml += $xml.InnerXml
$finalXml += "</path>"
([xml]$finalXml).Save($exportPath)

我确实找到this question但是它并没有解决添加方式,以便在组合文件中告知每个文件来源的位置。

1 个答案:

答案 0 :(得分:1)

正如您已经发现的那样,Powershell的XML功能强大但非常敏感。老实说,我现在还不完全理解它,但这个操作非常简单。我发现在处理子节点和最终产品作为XmlElement对象时,XmlDocument对象类型最容易使用。 (<variable>.<node>结构意味着XmlElement

$PathList = "C:\Test\1.xml","C:\Test\2.xml"
$exportPath = 'C:\Test\combined.xml'

#Cheating a bit, but we don't need much
$finalXml = [xml]("<xml></xml>")

ForEach($Path in $PathList){
    $CurrentXml = [xml](Get-Content $Path)
    $CurrentXml.configuration.SetAttribute("path",$Path)

    <#
      Powershell's XML is touchy.
      Imports the original document into $finalXml's namespace, 
      then appends it to the first level inside $finalXml's document node.
    #>
    $importedNode = $finalXml.ImportNode($CurrentXml.DocumentElement, $true)
    $finalXml.DocumentElement.AppendChild($importedNode) 
}

$finalXml.Save($exportPath)

这不符合您的输出完全,但<webconfigs><path>节点可能是多余的,如果您需要保存所有输出。它应该是这样的:

<xml>
    <configuration path="D:\pathtoxml\1.xml">
        ...
    </configuration>
    <configuration path="D:\pathtoxml\2.xml">
        ...
    </configuration>
</xml>

参考文献:

XmlElement class

XmlDocument class