【问题标题】:Compare XML for Differences比较 XML 的差异
【发布时间】:2016-09-04 04:46:42
【问题描述】:

我正在编写一些 C# 代码,目的是定期下载 XML 文件,将生成的 XML 与先前下载的 XML 版本进行比较(以检测更改),然后通过“操作”更改(通过适当的 CRUD 语句)更新反映 XML 中实体的数据库记录。

我需要一些关于“将生成的 XML 与以前下载的版本进行比较以检测更改”部分要求的帮助。

所以...考虑以下两个有细微差别的 XML 文档...

原创

<ROOT>
   <Stock>
      <Vehicle id="2574074">
         <DealerName>Super Cars London</DealerName>
         <FriendlyName>Ford Ranger 3.2 double cab 4x4 XLT auto</FriendlyName>
         <ModelName>Ranger</ModelName>
         <MakeName>Ford</MakeName>
         <Registration>DG55TPG</Registration>
         <Price>40990</Price>
         <Colour>WHITE</Colour>
         <Year>2014</Year>
         <Mileage>52000</Mileage>
         <Images>
            <Image Id="4771304" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=4771304&amp;Type=6&amp;Width=60&amp;Height=60&amp;FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=4771304&amp;Type=6&amp;Width=640&amp;FeedId=42" LastModified="2016-02-02T08:24:51.48" Priority="1"/>
         </Images>
      </Vehicle>
      <Vehicle id="2648665">
         <DealerName>Super Cars London</DealerName>
         <FriendlyName>BMW 320i</FriendlyName>
         <ModelName>3 Series</ModelName>
         <MakeName>BMW</MakeName>
         <Registration>CN03YZG</Registration>
         <Price>24990</Price>
         <Colour>WHITE</Colour>
         <Year>2013</Year>
         <Mileage>96000</Mileage>
         <Images/>
      </Vehicle>
   </Stock>
</ROOT>

<ROOT>
   <Stock>
      <Vehicle id="2575124">
         <DealerName>Supercars London</DealerName>
         <FriendlyName>Ford Ranger 3.2 double cab 4x4 XLT auto</FriendlyName>
         <ModelName>Ranger</ModelName>
         <MakeName>Ford</MakeName>
         <Registration>DK08FKP</Registration>
         <Price>43990</Price>
         <Colour>WHITE</Colour>
         <Year>2014</Year>
         <Mileage>30000</Mileage>
         <Images>
            <Image Id="5119812" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5119812&amp;Type=6&amp;Width=60&amp;Height=60&amp;FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5119812&amp;Type=6&amp;Width=640&amp;FeedId=42" LastModified="2016-04-11T13:08:42.81" Priority="1"/>
         </Images>
      </Vehicle>
      <Vehicle id="2648665">
         <DealerName>Super Cars London</DealerName>
         <FriendlyName>BMW 320i</FriendlyName>
         <ModelName>3 Series</ModelName>
         <MakeName>BMW</MakeName>
         <Registration>CN03YZG</Registration>
         <Price>24990</Price>
         <Colour>BRILLIANT WHITE</Colour>
         <Year>2013</Year>
         <Mileage>96000</Mileage>
         <Images>
            <Image Id="5201856" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201856&amp;Type=6&amp;Width=60&amp;Height=60&amp;FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201856&amp;Type=6&amp;Width=640&amp;FeedId=42" LastModified="2016-04-25T12:12:05.827" Priority="1"/>
            <Image Id="5201857" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201857&amp;Type=6&amp;Width=60&amp;Height=60&amp;FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201857&amp;Type=6&amp;Width=640&amp;FeedId=42" LastModified="2016-04-25T12:12:09.117" Priority="2"/>
            <Image Id="5201858" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201858&amp;Type=6&amp;Width=60&amp;Height=60&amp;FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201858&amp;Type=6&amp;Width=640&amp;FeedId=42" LastModified="2016-04-25T12:12:13.59" Priority="3"/>
            <Image Id="5201859" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201859&amp;Type=6&amp;Width=60&amp;Height=60&amp;FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201859&amp;Type=6&amp;Width=640&amp;FeedId=42" LastModified="2016-04-25T12:12:18.453" Priority="4"/>
            <Image Id="5201860" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201860&amp;Type=6&amp;Width=60&amp;Height=60&amp;FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201860&amp;Type=6&amp;Width=640&amp;FeedId=42" LastModified="2016-04-25T12:12:22.853" Priority="5"/>
         </Images>
      </Vehicle>
   </Stock>
</ROOT>

差异总结

  1. 车辆id="2575124" 不在原版中。这代表“创造”。
  2. 车辆id="2574074" 不在新车型中。这表示“删除”。
  3. 车辆id="2648665"(在原车和新车中都有)具有不同的&lt;Colour&gt;(白色 -> 亮白色)。这表示“更新”。
  4. 车辆id="2648665" 也有新的&lt;Image&gt; 节点。这表示“创建”(因为图像将与数据库中的车辆成 1:M)。

我查看了 XMLDiff 以生成带有添加/更改/删除指令的 DiffGram,但我看不出有一种方法可以让它生成代表我总结的更改的 DiffGram,例如它将更改 1 和 2 视为“更改” - &lt;xd:change match="@id"&gt;2648665&lt;/xd:change&gt; - 而不是缺少和添加车辆。

有没有办法用 XMLDiff 做到这一点?

或者,有没有“更好”的方法来实现我正在寻找的结果?

【问题讨论】:

    标签: c# xml diff


    【解决方案1】:

    我发现 LINQ 可以很好地解析 XML 的差异,例如...

    XDocument xNewVehicle = new XDocument(new XElement("UsedStock",
                        from newVehicle in newXml.Descendants("Vehicle")
                        join oldVehicle in oldXml.Descendants("Vehicle")
                            on newVehicle.Attributes("id").First().Value equals oldVehicle.Attributes("id").First().Value into oldVehicles
                        where !oldVehicles.Any()  // where the vehicle exists in new but not in old
                            || newVehicle.ToString() != oldVehicles.First().ToString()  // where the new vehicle is not the same as the old vehicle
                    select newVehicle));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多