2014-01-28 20 views
0

我需要組合幾個XML文件,並告訴這部分屬於每個文件的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

你爲什麼不告訴我們第一本 「各種XML的錯誤」 的是,我們可以着手解決這個問題。 – LarsH

回答

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