简体   繁体   English

如何比较两个 XElement 枚举?

[英]How to compare two XElement enumerables?

I have two equal sized lists of XElements:我有两个相同大小的 XElements 列表:

var documentDatabase = XDocument.Parse(xmlDatabase);
var rootElementDatabase = documentDatabase.Root;
var segmentsDatabase = rootElementDatabase.Descendants("Segment");

var documentSlave = XDocument.Parse(xmlSlave);
var rootElementSlave = documentSlave.Root;
var segmentsSlave = rootElementSlave.Descendants("Segment");

Basically list of Segment elements, like below:基本上是 Segment 元素列表,如下所示:

        <Segment>
            <IdRef>1</IdRef>
            <Start>
                <Master>0</Master>
                <Slave>0</Slave>
                <PntType>4</PntType>
            </Start>
            <End>
                <Master>10.059000</Master>
                <Slave>29.450302</Slave>
                <PntType>4</PntType>
            </End>
            <Symmetry>0.5</Symmetry>
            <FunctionType>1</FunctionType>
        </Segment>

What I want is to compare whether they equal by comparing values in <Master> and <Slave> tags, ignoring the rest.我想要的是通过比较<Master><Slave>标签中的值来比较它们是否相等,忽略其余部分。 How can I achieve it?我怎样才能实现它?

If you have multiple segments create a method to compare them如果您有多个细分,请创建一个方法来比较它们

Private Function ChkSeg(segment As XElement) As Boolean
    If segment.<Start>.<Master>.Value = segment.<End>.<Master>.Value AndAlso
         segment.<Start>.<Slave>.Value = segment.<End>.<Slave>.Value Then
        Return True
    Else
        Return False
    End If
End Function

then iterate the XML like this然后像这样迭代 XML

    For Each el As XElement In xe.Elements
        If ChkSeg(el) Then
            Stop
        Else
            Stop
        End If
    Next

Tested using this使用此测试

    Dim xe As XElement

    xe = <segments>
             <Segment>
                 <IdRef>1</IdRef>
                 <Start>
                     <Master>0</Master>
                     <Slave>0</Slave>
                     <PntType>4</PntType>
                 </Start>
                 <End>
                     <Master>10.059000</Master>
                     <Slave>29.450302</Slave>
                     <PntType>4</PntType>
                 </End>
                 <Symmetry>0.5</Symmetry>
                 <FunctionType>1</FunctionType>
             </Segment>
             <Segment>
                 <IdRef>1</IdRef>
                 <Start>
                     <Master>0</Master>
                     <Slave>0</Slave>
                     <PntType>4</PntType>
                 </Start>
                 <End>
                     <Master>0</Master>
                     <Slave>0</Slave>
                     <PntType>4</PntType>
                 </End>
                 <Symmetry>0.5</Symmetry>
                 <FunctionType>1</FunctionType>
             </Segment>
         </segments>

If you want to compare the Master and Slave tags in both the Start and the End, you can use this method:如果要比较Start和End中的Master和Slave标签,可以使用这种方法:

bool CompareXmls(XElement first, XElement second)
{
        var firstStart = first.Element("Start");
        var firstEnd = first.Element("End");
        var firstStartMaster = firstStart.Element("Master").Value;
        var firstStartSlave = firstStart.Element("Slave").Value;
        var firstEndMaster = firstEnd.Element("Master").Value;
        var firstEndSlave = firstEnd.Element("Slave").Value;

        var secondStart = second.Element("Start");
        var secondEnd = second.Element("End");
        var secondStartMaster = secondStart.Element("Master").Value;
        var secondStartSlave = secondStart.Element("Slave").Value;
        var secondEndMaster = secondEnd.Element("Master").Value;
        var secondEndSlave = secondEnd.Element("Slave").Value;

        bool areEqual = firstStartMaster == secondStartMaster
            && firstStartSlave == secondStartSlave
            && firstEndMaster == secondEndMaster
            && firstEndSlave == secondEndSlave;

        return areEqual;
    }

EDIT: Wanting to compare lists which are already sorted and you can compare first item from one list with first item from other list and so on, you can use LINQ .Zip method to group the XElements into one anonymous object and then using LINQ .All cycle through the items and compare:编辑:想要比较已经排序的列表,并且您可以将一个列表中的第一个项目与另一个列表中的第一个项目进行比较,依此类推,您可以使用 LINQ .Zip 方法将 XElements 分组为一个匿名对象,然后使用 LINQ .All循环遍历项目并比较:

var res = firstList.Zip(secondList, (a, b) => new { First = a, Second = b }).All(x=>CompareXmls(x.First,x.Second));

Personally, I would probably use a for loop to iterate over the two lists and compare each item, but if you want to use a one-liner LINQ, you can use the said methods.就个人而言,我可能会使用 for 循环来遍历两个列表并比较每个项目,但如果您想使用单行 LINQ,则可以使用上述方法。

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

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