我正在编写一些C#代码,目的是定期下载XML文件,将生成的XML与先前下载的XML版本进行比较(以检测更改),然后通过适当的CRUD语句“执行”更改(通过适当的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&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=4771304&Type=6&Width=640&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&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5119812&Type=6&Width=640&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&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201856&Type=6&Width=640&FeedId=42" LastModified="2016-04-25T12:12:05.827" Priority="1"/>
<Image Id="5201857" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201857&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201857&Type=6&Width=640&FeedId=42" LastModified="2016-04-25T12:12:09.117" Priority="2"/>
<Image Id="5201858" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201858&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201858&Type=6&Width=640&FeedId=42" LastModified="2016-04-25T12:12:13.59" Priority="3"/>
<Image Id="5201859" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201859&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201859&Type=6&Width=640&FeedId=42" LastModified="2016-04-25T12:12:18.453" Priority="4"/>
<Image Id="5201860" ThumbUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201860&Type=6&Width=60&Height=60&FeedId=42" FullUrl="http://www.somewhere.com/GetImage.aspx?ImageId=5201860&Type=6&Width=640&FeedId=42" LastModified="2016-04-25T12:12:22.853" Priority="5"/>
</Images>
</Vehicle>
</Stock>
</ROOT>
差异摘要
id="2575124"
。这代表了“创造”。id="2574074"
。这代表“删除”。id="2648665"
(包含原始版本和新版本)具有不同的<Colour>
(WHITE - &gt; BRILLIANT WHITE)。这代表“更新”。id="2648665"
在新版本中也有新的<Image>
个节点。这表示“创建”(因为数据库中的车辆图像将为1:M)。我已经查看了XMLDiff来生成带有添加/更改/删除指令的DiffGram但是我看不到让它生成表示我总结的更改的DiffGram的方法,例如它将变更1和2视为“变更” - <xd:change match="@id">2648665</xd:change>
- 而不是缺少和添加车辆。
有没有办法用XMLDiff做到这一点?
或者,有没有“更好”的方法来实现我正在寻找的结果?
答案 0 :(得分:0)
我发现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));