How to compare 2 xml files in C# and output the differences in my desired format?

629 Views Asked by At

I have 2 xml files want to compare them by using C#. 2 xml might in different order

file1:

<PartPriceInfo xmlns:ns1="http://www.Car.com">
   <ns1:Car model="Legacy">
      <ns1:Muffler> 1 </ns1:Muffler>
      <ns1:Bumper> 2 </ns1:Bumper>
   </ns1:Car>
   <ns1:Car model="Outback">
      <ns1:Muffler> 3 </ns1:Muffler>
      <ns1:Bumper> 4</ns1:Bumper>
   </ns1:Car>
   <ns1:Car model="FullSize">
      <ns1:Muffler> 5 </ns1:Muffler>
      <ns1:Bumper> 6 </ns1:Bumper>
   </ns1:Car>
</PartPriceInfo>

file2:

<PartPriceInfo xmlns:ns1="http://www.Car.com">
   <ns1:Car model="Outback">
      <ns1:Muffler> 3 </ns1:Muffler>
      <ns1:Bumper> 5</ns1:Bumper>
   </ns1:Car>
   <ns1:Car model="Legacy">
        <ns1:Muffler> 1 </ns1:Muffler>
        <ns1:Bumper> 2 </ns1:Bumper>
   </ns1:Car>
   <ns1:Car model="MidSize">
      <ns1:Muffler> 7 </ns1:Muffler>
      <ns1:Bumper> 8 </ns1:Bumper>
   </ns1:Car>
</PartPriceInfo>

My expected result is something like below I need all the status like "updated" "added" "deleted" with specific information in it.

<ns1:status= "Updated">
    <ns1:Car model="Outback">
        <ns1:Muffler> 5</ns1:Muffler>
    </ns1:Car>
</ns1:status>
<ns1:status= "Deleted">
<ns1:Car model="FullSize">
    <ns1:Muffler> 5 </ns1:Muffler>
    <ns1:Bumper> 6 </ns1:Bumper>
</ns1:Car>
</ns1:status>
<ns1:status= "Added">
    <ns1:Car model="MidSize">
        <ns1:Muffler> 7 </ns1:Muffler>
        <ns1:Bumper> 8 </ns1:Bumper>
    </ns1:Car>
</ns1:statues>

Does anyone have any idea how to do this in C#? Any help would be appreciated.

0

There are 0 best solutions below