如何区分 xdocument1 和 xdocument2

How to get difference between xdocument1 and xdocument2

我想了解 xdocument1 和 xdocument2 之间的区别。 在 xdocument1 上:

<prices>
   <price>
     <productid>P001</productid>
     <price>1000</price>
     <effectivedate>2015-05-11T00:00:00+7</effectivedate>
   </price>
</prices>

在 xdocument2 上:

<prices>
   <price>
     <productid>P001</productid>
     <price>870</price>
     <effectivedate>2015-05-11T00:00:00+7</effectivedate>
   </price>
</prices>

如何区分它们,如果effectivedate和productid的值相同,那么应该return没有价格记录,像这样:

<prices/>

如果生效日期不同,例如:

在 xdocument1 上:

<prices>
   <price>
     <productid>P001</productid>
     <price>1000</price>
     <effectivedate>2015-05-12T00:00:00+7</effectivedate>
   </price>
</prices>

在 xdocument2 上:

<prices>
   <price>
     <productid>P001</productid>
     <price>870</price>
     <effectivedate>2015-05-11T00:00:00+7</effectivedate>
   </price>
</prices>

那么价格标签应该从xdocument1中获取,return:

<prices>
   <price>
     <productid>P001</productid>
     <price>1000</price>
     <effectivedate>2015-05-12T00:00:00+7</effectivedate>
   </price>
</prices>

我想建议以下步骤

  1. 使用以 ProductID 作为键和包含 effectivedateprice 的对象的 hashmap。
  2. 对象可以具有重写的 equals 方法以进行有效比较。
  3. 首先加载哈希图中的任一文档,然后检查与第二个文档的相似性或差异性,并相应地返回结果。
  4. 这需要线性时间 O(n+m),其中 n 是第一个文档元素的 size/number,m 是第二个文档元素的 size/number,以找到所有差异。

我更像是一个 java 人。稍后我可以在 Java 中提供代码片段。

这似乎如您所愿。

var query =
    new XDocument(
        new XElement(
            "prices",
            from p1 in xdocument1.Root.Elements("price")
            join p2 in xdocument2.Root.Elements("price")
                on p1.Element("productid").Value equals p2.Element("productid").Value
            where p1.Element("effectivedate").Value != p2.Element("effectivedate").Value
            select p1));

看起来很简单所以如果我遗漏了什么请告诉我。