比较两个 xml 并使用 LINQ 打印差异
我正在比较两个 xml,并且必须打印差异。我怎样才能使用 LINQ 来实现这一点。 我知道我可以使用 Microsoft 的 XML diff 补丁,但我更喜欢使用 LINQ 。如果您有任何其他想法,我将实现
//First Xml
<Books>
<book>
<id="20504" image="C01" name="C# in Depth">
</book>
<book>
<id="20505" image="C02" name="ASP.NET">
</book>
<book>
<id="20506" image="C03" name="LINQ in Action ">
</book>
<book>
<id="20507" image="C04" name="Architecting Applications">
</book>
</Books>
//Second Xml
<Books>
<book>
<id="20504" image="C011" name="C# in Depth">
</book>
<book>
<id="20505" image="C02" name="ASP.NET 2.0">
</book>
<book>
<id="20506" image="C03" name="LINQ in Action ">
</book>
<book>
<id="20508" image="C04" name="Architecting Applications">
</book>
</Books>
我想比较这两个 xml 并像这样打印结果。
Issued Issue Type IssueInFirst IssueInSecond
1 image is different C01 C011
2 name is different ASP.NET ASP.NET 2.0
3 id is different 20507 20508
I am comparing two xml and I have to print the difference. How can I achieve this using LINQ.
I know I can use XML diff patch by Microsoft but I prefer to use LINQ . If you have any other idea I will implement that
//First Xml
<Books>
<book>
<id="20504" image="C01" name="C# in Depth">
</book>
<book>
<id="20505" image="C02" name="ASP.NET">
</book>
<book>
<id="20506" image="C03" name="LINQ in Action ">
</book>
<book>
<id="20507" image="C04" name="Architecting Applications">
</book>
</Books>
//Second Xml
<Books>
<book>
<id="20504" image="C011" name="C# in Depth">
</book>
<book>
<id="20505" image="C02" name="ASP.NET 2.0">
</book>
<book>
<id="20506" image="C03" name="LINQ in Action ">
</book>
<book>
<id="20508" image="C04" name="Architecting Applications">
</book>
</Books>
I want to compare this two xml and print result like this.
Issued Issue Type IssueInFirst IssueInSecond
1 image is different C01 C011
2 name is different ASP.NET ASP.NET 2.0
3 id is different 20507 20508
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案如下:
两个 linq 语句可能可以合并,但我将其作为练习留给您。
Here is the solution:
Both linq statements probably could be merged, but I leave that as an exercise for you.
为了好玩,提供了grega g阅读问题的通用解决方案。为了说明我对这种方法的反对,我为“PowerShell in Action”引入了一个“正确”条目。
其报告如下:
请注意,最后两个条目是 20508 拼写错误与其他正确的 20508 条目之间的“冲突”。
For fun, a general solution to grega g's reading of the problem. To illustrate my objection to this approach, I've introduced a "correct" entry for 'PowerShell in Action'.
Which reports the following:
Note that the last two entries are the "conflict" between the 20508 typo and the otherwise correct 20508 entry.
这里您想要的操作是一个 Zip 操作,用于将两个书籍序列中的相应元素配对。该运算符正在 在 .NET 4.0 中添加,但我们可以通过使用 Select 来获取书籍索引并加入它来伪造它:
然后我们将使用第二个连接按名称比较属性值。请注意,这是一个内部联接;如果您确实想包含其中一个或另一个缺少的属性,您将需要做更多的工作。
结果将是一个嵌套集合:
或者获取每本书的多行:
报告以下内容:
The operation you want here is a Zip to pair up corresponding elements in your two sequences of books. That operator is being added in .NET 4.0, but we can fake it by using Select to grab the books' indices and joining on that:
We'll then use a second join to compare the values of attributes by name. Note that this is an inner join; if you did want to include attributes missing from one or the other you would have to do quite a bit more work.
The result will be a nested collection:
Or to get multiple rows per book:
Which reports the following: