2012-11-04 39 views
3

我有兩個xml配置文件,我只需要比較兩個文件的結構並顯示其差異。 請注意:比較時,應該忽略xml節點中的值。比較xml結構使用Powershell

例如:

XML 1 
---- 
<recipe> 
    <ingredients> 
     <ingredient1></ingredient1><ingredient2></ingredient2> 
    </ingredients> 
    <description></description> 
</recipe> 

XML 2 
----- 
<recipe> 
    <ingredients> 
    <ingredient1></ingredient1> 
    </ingredients> 
    <description></description> 
    <images></images> 
</recipe> 

結果應該是兩個XML文件的差異。

xml1 <ingredient2> 
xml2 <images> 

非常感謝。

回答

7

我能想出快捷的解決方案是:

[xml]$xml1 = @" 
<recipe> 
    <ingredients> 
     <ingredient1> 
     </ingredient1> 
     <ingredient2> 
     </ingredient2> 
    </ingredients> 
    <description></description> 
</recipe> 
"@ 

[xml]$xml2 = @" 
<recipe> 
    <ingredients> 
    <ingredient1>dada</ingredient1> 
    </ingredients> 
    <description>dadad</description> 
    <images></images> 
</recipe> 
"@ 

$docDiffs=Compare-Object ($xml1.SelectNodes("//*") | Select-Object -Expand Name) ($xml2.SelectNodes("//*") | Select-Object -Expand Name) 

$docDiffs 

你需要做的工作一點點地得到你所需要的確切格式化,但主要工作已經完成。如果你想改進它,你可以選擇使用Select-XML。

+0

工程太棒了!謝謝。 – jack

+0

很高興它可以幫助你 –