Finding Matching Nodes in two XML files using linqpad and linq to xml is finding 0 results

我们两清 提交于 2020-01-06 08:23:48

问题


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.

Here is my code...

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()                          

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.

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

(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.

What am I missing here?


回答1:


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

or compare them in matchingResults as

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();


来源:https://stackoverflow.com/questions/20203593/finding-matching-nodes-in-two-xml-files-using-linqpad-and-linq-to-xml-is-finding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!