How to compare two xml files and display the difference side by side.

New Contributor

$baseServer="C:\Store\PS\referrencexml.xml"
$Server2Compare="C:\Store\PS\differencexml.xml"

$boutput = Compare-Object -ReferenceObject (Get-Content -Path "$baseServer") -DifferenceObject (Get-Content -Path "$Server2Compare")

 

Above command shows the difference between two files in the below format(Output1), but I need to display the difference in side by side like Output2. So, if someone could help me with an idea or sample code/script to get the output in two different column side by side, it will be very helpful.

 

Output1:

Input Object                         Side Indicator

<genre></genre>                 =>

<genre>Fantasy</genre>     <=

 

Output 2:

Reference File                                                                  Difference File

<genre></genre>                                                <genre>Fantasy</genre>

6 Replies

@jos07 

 

Not sure if that's possible, but it's PowerShell so anything is ;), but you could try this to show the differences in the second file

(https://stackoverflow.com/questions/49997190/powershell-compare-two-xml-files-and-only-get-the-value...)

$original = Compare-Object $XML1 $XML2 | Where-Object{ $_.SideIndicator -eq "=>" } | ForEach-Object{ $_.InputObject }

 

 

PowerShell Compare-Object is giving you a text comparison line-by-line of the files that you have passed it, so sees one line in the reference object and a different line in the difference object - it isn't aware that they are the same part of the xml and that the value is the difference.
Is this comparison a one-off or something you need to repeat/automate? If its a one off then perhaps a text editor like VSCode or Notepadd++ will let you compare the files ?
If you need to automate then I think you might need to think beyond XML so that the descriptive structure of the data is not being compared - perhaps as a csv file or something. Can you share more of the xml, is it very complex?

@jos07 

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

@Harm_Veenstra

Thank you for your reply. Yeah I could get the difference of other file in the new variable/file, but again combining it and display it side by side is something I'm not able to do.
Thanks for your reply Jonathan.
It's not a one time thing, I have to repeat this validation also automate it. As you presumed it's a complex XML. Even I tried with comparing JSON files, but again I'm ending at the same problem, it's just compare-object function not able to print the output difference side by side, and i'm not aware of how to do it in alternate way or customized way.
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.