2012-11-04 38 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

很高兴它可以帮助你 –