返回不在其他XML中的元素

时间:2012-03-21 13:17:26

标签: c# xml linq

我有2个XElements。每个包含多个子元素

例如

XML1:

<Addresses>
  <Address>
   <Line1>1 Street</Line1>
   <Postcode>PL1 5RT</Postcode>
  </Address>
  <Line1>57 New Street</Line1>
   <Postcode>PL1 5RT</Postcode>
  </Address>
  <Address>
   <Line1>2 Street</Line1>
   <Postcode>PL1 5RT</Postcode>
  </Address>
</Addresses>

XML2:

<Addresses>
  <Address>
   <Line1>1 Street</Line1>
   <Postcode>PL1 5RT</Postcode>
  </Address>
  <Address>
   <Line1>2 Street</Line1>
   <Postcode>PL1 5RT</Postcode>
  </Address>
</Addresses>

我正在尝试整理一个linq查询,该查询将过滤掉Xml中但不在Xml2中的地址元素(在上面的例子中,它将是地址“57 New Street”)

目前我的代码如下:

var diffAddress = from address1 in Xml1.Elements()
                  from address2 in Xml2.Elements()
                  where (string)address1.Element("Line1") != (string)address2.Element("Line1") || 
                  where (string)address1.Element("Postcode") != (string)address2.Element("Postcode")
                  select address1;

然而它返回Xml1中的所有值 我是否正确地认为我可以通过单个查询执行此操作,或者我是否必须从两者中获取结果然后迭代它们以获取Xml1中不在Xml2中的地址?

非常感谢任何帮助:)

3 个答案:

答案 0 :(得分:1)

使用匿名查询可以让生活更轻松:

var addresses2 = from xaddr in x2.Root.Elements("Address")
                 let a = new
                         {
                             Line1 = xaddr.Element("Line1").Value,
                             PostalCode = xaddr.Element("Postcode").Value
                         }
                 select a;

// take the addresses in the first XML which are found in the second XML
// nota bene: this is case sensitive.
var addresses = (from xaddr in x1.Root.Elements("Address")
                 let a = new
                         {
                             Line1 = xaddr.Element("Line1").Value,
                             PostalCode = xaddr.Element("Postcode").Value
                         }
                 select a)
                .Intersect(addresses2);

答案 1 :(得分:0)

你需要做一个子查询 - 返回Xml1中Xml1中所有元素 - 然后查找Xml1中不在子查询中的所有元素。

答案 2 :(得分:0)

感谢保罗的信息!

以下是使用“Except”运算符

来符合我的解决方案的代码
    var newAddresses= Xml1.Descendants("Address").Cast<XNode>()
                      .Except(Xml2.Descendants("Address").Cast<XNode>(), new XNodeEqualityComparer());