简体   繁体   English

如何使用Linq比较两个XDocument之间的元素?

[英]How to compare elements between two XDocument's by using Linq?

I try to compare elements from two different XDocument's where some XElements match with each other in Linq and I also wanted to try out how you can show the elements in 'srcTree' that is not in 'srcTree2'. 我尝试比较来自两个不同XDocument的元素,其中在Linq中某些XElement彼此匹配,并且我还想尝试如何显示“ srcTree”中而不是“ srcTree2”中的元素。 I tried before with a 'where' from Linq, but without any luck unfortunately. 我之前曾尝试过Linq的“ where”,但不幸的是没有运气。

My code below: 我的代码如下:

class LearnXML {
    static void Main() {
        XDocument srcTree = new XDocument(
            new XComment("This is a comment"),
            new XElement("Root",
                new XElement("Child", "data1"),
                new XElement("Child", "data2"),
                new XElement("Child", "data3"),
                new XElement("Child", "data4"),
                new XElement("Info", "info5"),
                new XElement("Info", "info6"),
                new XElement("Info", "info7"),
                new XElement("Info", "info8")
            )
        );

        XDocument srcTree2 = new XDocument(
            new XComment("This is a comment"),
            new XElement("Root",
                new XElement("Child", "data1"),
                new XElement("Child", "data4"),
                new XElement("Info", "info6"),
                new XElement("Info", "info8")
            )
        );

        Console.WriteLine(srcTree);

        XDocument doc = new XDocument(
            new XComment("This is a comment"),
            new XElement("Root",
                from el in srcTree2.Element("Root").Elements()
                join rp in srcTree.Element("Root").Elements()
                on !el.Element("Child").Value equals rp.Element("Child").Value
                select el
            )
        );

        Console.WriteLine(doc);
    }
}

Problem in your code is that you try to join two Roots by equality and then inverse it. 代码中的问题是您尝试相等地连接两个Roots ,然后对其求逆。 So your code not even compile. 因此,您的代码甚至无法编译。

You can retrieve a difference between two Roots using a nested query. 您可以使用嵌套查询检索两个Roots之间的差异。 So the code below retrieve all differences nodes not only "Child" node. 因此,下面的代码将检索所有差异节点,而不仅仅是"Child"节点。

      XDocument doc = new XDocument(
            new XComment("This is a comment"),
            new XElement("Root",
                from left in srcTree.Element("Root").Elements()
                where left != null && !(from right in srcTree2.Element("Root").Elements()
                                        where right != null
                                        select right.Value).Contains(left.Value)
                select left)
        );

If you really want to retrieve difference between nodes "Child" just get elements by name: 如果您确实想检索节点"Child"之间的差异,只需按名称获取元素:

        XDocument doc = new XDocument(
            new XComment("This is a comment"),
            new XElement("Root",
                from left in srcTree.Element("Root").Elements("Child")
                where left != null && !(from right in srcTree2.Element("Root").Elements("Child")
                                        where right != null
                                        select right.Value).Contains(left.Value)
                select left)
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM