首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何区分xdocument1和xdocument2

如何区分xdocument1和xdocument2
EN

Stack Overflow用户
提问于 2015-05-08 15:57:55
回答 2查看 94关注 0票数 1

我想知道xdocument1和xdocument2之间的区别。在xdocument1上:

代码语言:javascript
运行
复制
<prices>
   <price>
     <productid>P001</productid>
     <price>1000</price>
     <effectivedate>2015-05-11T00:00:00+7</effectivedate>
   </price>
</prices>

在xdocument2上:

代码语言:javascript
运行
复制
<prices>
   <price>
     <productid>P001</productid>
     <price>870</price>
     <effectivedate>2015-05-11T00:00:00+7</effectivedate>
   </price>
</prices>

如何才能得到它们之间的区别,如果有效产品和产品具有相同的价值,那么它就不应该返回价格记录,如下所示:

代码语言:javascript
运行
复制
<prices/>

如果效果不同,如下所示:

在xdocument1上:

代码语言:javascript
运行
复制
<prices>
   <price>
     <productid>P001</productid>
     <price>1000</price>
     <effectivedate>2015-05-12T00:00:00+7</effectivedate>
   </price>
</prices>

在xdocument2上:

代码语言:javascript
运行
复制
<prices>
   <price>
     <productid>P001</productid>
     <price>870</price>
     <effectivedate>2015-05-11T00:00:00+7</effectivedate>
   </price>
</prices>

然后价格标签应该从xdocument1得到,返回:

代码语言:javascript
运行
复制
<prices>
   <price>
     <productid>P001</productid>
     <price>1000</price>
     <effectivedate>2015-05-12T00:00:00+7</effectivedate>
   </price>
</prices>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-09 04:36:15

这看起来像你想要的那样起作用。

代码语言:javascript
运行
复制
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));

这似乎很简单,所以如果我错过了什么,请告诉我。

票数 1
EN

Stack Overflow用户

发布于 2015-05-08 19:05:22

我想建议以下步骤

  1. 使用以ProductID为键和对象的hashmap,包含有效和价格。
  2. 对象可以有一个重写的相等方法来进行有效的比较。
  3. 首先在hashmap中加载文档中的任何一个,然后与第二个文档检查相似度或差异,然后返回相应的结果。
  4. 这需要线性时间O(n+m),其中n是第一个doc的大小/元素数,m是第二个doc的大小/元素数,以找到所有的差异。

我更像一个爪哇人。我可以在某个时候用Java提供一个代码片段。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30128098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档