Forum Discussion
jos07
Feb 01, 2022Copper Contributor
How to compare two xml files and display the difference side by side.
$baseServer="C:\Store\PS\referrencexml.xml" $Server2Compare="C:\Store\PS\differencexml.xml" $boutput = Compare-Object -ReferenceObject (Get-Content -Path "$baseServer") -DifferenceObject (Get-Conte...
Jonathan_Allen
Feb 01, 2022Brass Contributor
I did some more experimenting ... hope the code comments explain what is being done
# set up two xml variables
# the equivalent for you would be [xml] (get-content <xmlfilepath>)
[xml]$doc1 = @"
<geography>
<country>
<name>UK</name>
<capital>London</capital>
</country>
<country>
<name>France</name>
<capital>Paris</capital>
</country>
</geography>
"@
[xml]$doc2 = @"
<geography>
<country>
<name>Germany</name>
<capital>Berlin</capital>
</country>
<country>
<name>UK</name>
<capital>London</capital>
</country>
<country>
<name>Italy</name>
<capital>Rome</capital>
</country>
</geography>
"@
# because the two variables are [xml] data type we can now use xml notation to reference the parts we want to compare - here compare country names
compare-object -referenceobject $doc1.geography.country.name -differenceobject $doc2.geography.country.name
<#
InputObject SideIndicator
----------- -------------
Germany =>
Italy =>
France <=
#>
# here compare country capitals
compare-object -referenceobject $doc1.geography.country.capital -differenceobject
$doc2.geography.country.capital
<#
InputObject SideIndicator
----------- -------------
Berlin =>
Rome =>
Paris <=
#>
# now there is also the Select-Xml which is a bit less specific
$Doc1Capitals = Select-Xml -Xml $doc1 -XPath "//capital"| foreach {$_.node.InnerXML}
$Doc2Capitals = Select-Xml -Xml $doc2 -XPath "//capital"| foreach {$_.node.InnerXML}
# so when we compare those varaibles we can get the same output
compare-object $Doc1Capitals $Doc2Capitals
<#
InputObject SideIndicator
----------- -------------
Berlin =>
Rome =>
Paris <=
#>
hope this gives you a boost but I fear you are working with complex XML and the parsing and evaluation will be difficult
jos07
Feb 02, 2022Copper Contributor
Again, thanks for sharing the code.
Somehow I managed parsing and evaluation of the XML file. Yeah it's a very complex XML file and has lot of data, so the requirement is to get the differences side by side so that it will be easy for validation.
If this can be achieved by using other export option or altering other commands and logic, that's also fine. If you have some idea, please let me know.
Somehow I managed parsing and evaluation of the XML file. Yeah it's a very complex XML file and has lot of data, so the requirement is to get the differences side by side so that it will be easy for validation.
If this can be achieved by using other export option or altering other commands and logic, that's also fine. If you have some idea, please let me know.