Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse an XML diff to show only differences

Tags:

c#

xml

I compare the xml of two serialized objects using the MS XmlDiffPatch tool. (C#)

XML Sample 1:

<HotelBookingView xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
  <Id>119</Id> 
  <RoomId>1</RoomId> 
  <NumberNights>4</NumberNights>
  <CourseBookings>       
   <CourseHotelLink> 
    <Id>0</Id> 
   </CourseHotelLink> 
 </CourseBookings> 
</HotelBookingView>

XML Sample 2:

<HotelBookingView xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
  <Id>119</Id> 
  <RoomId>1</RoomId> 
  <NumberNights>5</NumberNights>
  <CourseBookings>       
   <CourseHotelLink> 
    <Id>0</Id> 
   </CourseHotelLink> 
 </CourseBookings> 
</HotelBookingView>

(NumberNights has changed from 4 to 5)

The Diff between them:

<?xml version="1.0" encoding="utf-8" ?> 
  <xd:xmldiff version="1.0" srcDocHash="14315823970661993399" options="IgnoreChildOrder" fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff">
  <xd:node match="1">
   <xd:node match="3">
    <xd:change match="1">5</xd:change> 
   </xd:node>
  </xd:node>

Any advice on how to process the diff to be able to show only the differences between the two?

I'd like a final display that just shows NumberNights: oldvalue (4), new value (5)

The Xml Diff Tool has a GetHtml() function but it displays all the values and just highlights those that have changed. Currently to get just the difference I look through the produced html for the CSS 'lightgreen', then do string-manipulation to find the name of the node.. which is insane and really slow for a table full of audit data! Any better ideas appreciated!

like image 575
UglyTeapot Avatar asked May 10 '12 08:05

UglyTeapot


1 Answers

Are you determined to use XmlDiffPatch? We have used xmldiff which has more meaningful output (in my opinion). In your above example, the output would be something like

<HotelBookingView xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:diff="http://www.via.ecp.fr/~remi/soft/xml/xmldiff" diff:status="below"> 
  <NumberNights diff:status="modified">4|5</NumberNights>
</HotelBookingView>

This keeps the tag structure, indicates which tags have modified content, and shows the previous/current values separated by a pipe (|) character.

like image 89
Rob Scott Avatar answered Oct 31 '22 12:10

Rob Scott