使用linqpad和linq to XML在两个XML文件中查找匹配节点的结果为0
我有两个具有相同模式/结构但数据不同的XML文件。我试图使用Linqpad(Linq到XML)来查找这两个文件之间的差异 这是我的密码使用linqpad和linq to XML在两个XML文件中查找匹配节点的结果为0,linq,linq-to-xml,linqpad,Linq,Linq To Xml,Linqpad,我有两个具有相同模式/结构但数据不同的XML文件。我试图使用Linqpad(Linq到XML)来查找这两个文件之间的差异 这是我的密码 XElement FILE1 = XElement.Load (@"..\FILE1.XML"); XElement FILE2 = XElement.Load (@"..\FILE2.XML"); var orders = from file1 in FILE1.Descendants("Players").Elements("Player")
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()
最后一次转储()返回0个结果。我知道这两个文件中有匹配的数据
编辑我忘了提到,如果我转储每个查询的结果,我会得到两个序列的结果(非常相似)
我也尝试过这里显示的方法…(将文件组合成一个序列,然后进行比较)但我得到的结果是相同的…0个结果 这种方法似乎还可以在一阶序列上创建笛卡尔积 我只想从文件中找到匹配或丢失的节点
这里我遗漏了什么?问题是
matchingResults
正在比较XElement
(参考等式)-而不是string
(字符串内容)<代码>订单和订单2
正在选择firstName
和lastName
作为XElement
。因此,要获得您期望的结果,可以更改orders
和orders2
以选择firstName
和lastName
firstName = file1.Element("FirstName").Value
i.name.firstName.Value == j.name.firstName.Value
或者在匹配结果中比较它们
firstName = file1.Element("FirstName").Value
i.name.firstName.Value == j.name.firstName.Value
下面是使用第一个选项的完整示例:
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();
XElement文件1=XElement.Parse(
@"
牛仔
约翰史密斯
");
XElement文件2=XElement.Parse(
@"
牛仔
米克斯密斯
");
var orders=来自file1.substands(“Players”).Elements(“Player”)中的file1
选择新的{
名称=新{
firstName=file1.Element(“firstName”).Value,
lastName=file1.Element(“lastName”).Value
}
};
var orders2=来自file2.substands(“Players”).Elements(“Player”)中的file2
选择新的{
名称=新{
firstName=file2.Element(“firstName”).Value,
lastName=file2.Element(“lastName”).Value
}
};
//orders.Dump();
//orders2.Dump();
var matchingResults=从订单中的i到订单2中的j
其中(i.name.firstName==j.name.firstName&&i.name.lastName==j.name.lastName)
选择i;
matchingResults.Dump();
xml结构是什么样子的?现在,如果您想帮助回答一个更有趣的问题,那就太好了。