使用linqpad和linq to xml在两个XML文件中查找匹配节点将找到0个结果 [英] Finding Matching Nodes in two XML files using linqpad and linq to xml is finding 0 results

查看:84
本文介绍了使用linqpad和linq to xml在两个XML文件中查找匹配节点将找到0个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有相同架构/结构但数据不同的XML文件.我正在尝试使用Linqpad(Linq到XML)来查找两个文件之间的差异.

I have two XML files with the same schema/structure but with different data. I am trying to use Linqpad (Linq to XML) to find the differences between the two files.

这是我的代码...

XElement FILE1 = XElement.Load (@"..\FILE1.XML");
XElement FILE2 = XElement.Load (@"..\FILE2.XML");

var orders = from file1 in FILE1.Descendants("Players").Elements("Player")
                        select new {
                            name=new {
                                firstName=file1.Element("FirstName"),
                                lastName=file1.Element("LastName")
                            }                           
                        };

var orders2 = 
             from file2 in FILE2.Descendants("Players").Elements("Player")
                        select new {
                            name=new {
                                firstName=file2.Element("FirstName"),
                                lastName=file2.Element("LastName")
                            }                           
                        };

var matchingResults = from i in orders from j in orders2 where (i.name.firstName==j.name.firstName && i.name.lastName==j.name.lastName)
                            select i;
matchingResults.Dump()                          

最后一个Dump()返回0个结果.我知道两个文件中有匹配的数据.

The last Dump() is returning 0 results. I KNOW there is matching data in the two files.

编辑我忘了提一下,如果我转储每个查询的结果,则会得到两个序列的结果(非常相似).

EDIT I forgot to mention that if I dump the results of each of the queries I get results (that are very similar) for both sequences.

我也尝试过此处显示的方法...
比较两个xml并使用LINQ打印差异

I have also tried the approach shown here...
Compare two xml and print the difference using LINQ

(将文件组合成一个序列,然后进行比较),但是我得到的结果相同... 0个结果.

(which combines the files into one sequence and then does the compare) but I am geting the same result...0 results.

该方法似乎还可以在第一订单序列上创建笛卡尔积.

That approach also seems to create a cartesian product on the first orders sequence.

我想要做的就是从文件中找到匹配或丢失的节点.

All I want is to find matching or missing nodes from the files.

我在这里想念什么?

推荐答案

问题是matchingResults正在比较XElement(引用相等)而不是string(字符串内容). ordersorders2选择firstNamelastName作为XElement.因此,要获得您期望的结果,请更改ordersorders2以选择firstNamelastName作为

The problem is that matchingResults is doing a compare of XElement (reference equality) - not string (string contents). orders and orders2 are selecting firstName and lastName as XElement. So, to get what you expect, either change orders and orders2 to select firstName and lastName as

firstName = file1.Element("FirstName").Value

或在matchingResults中将它们进行比较

i.name.firstName.Value == j.name.firstName.Value

下面是使用第一个选项的完整示例:

Here's a full example using the first option:

XElement FILE1 = XElement.Parse(
@"<Root>
    <Players>
        <Player><FirstName>Bob</FirstName><LastName>Smith</LastName></Player>
        <Player><FirstName>John</FirstName><LastName>Smith</LastName></Player>
    </Players>
</Root>");
    XElement FILE2 = XElement.Parse(
@"<Root>
    <Players>
        <Player><FirstName>Bob</FirstName><LastName>Smith</LastName></Player>
        <Player><FirstName>Mike</FirstName><LastName>Smith</LastName></Player>
    </Players>
</Root>");

var orders = from file1 in FILE1.Descendants("Players").Elements("Player")
                    select new {
                        name=new {
                            firstName=file1.Element("FirstName").Value,
                            lastName=file1.Element("LastName").Value
                        }
                    };

var orders2 = from file2 in FILE2.Descendants("Players").Elements("Player")
                    select new {
                        name=new {
                            firstName=file2.Element("FirstName").Value,
                            lastName=file2.Element("LastName").Value
                        }
                    };

//orders.Dump();
//orders2.Dump();

var matchingResults = from i in orders from j in orders2
                                where (i.name.firstName == j.name.firstName && i.name.lastName == j.name.lastName)
                                select i;
matchingResults.Dump();

这篇关于使用linqpad和linq to xml在两个XML文件中查找匹配节点将找到0个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆