如何在ID列表中找到重复的ID

时间:2017-06-29 11:54:27

标签: xml vb.net

从我的vb代码中的xml在objXmlNode2中,我想检查重复的CaseEventInternalID。我怎么做?我应该能找到一个。

CaseEventInternalID位于strCaseEventInternalIDList列表中。我需要检查该列表中的重复项。

strCaseEventInternalIDList(i) = objXmlNode.SelectSingleNode("CaseEventInternalID").InnerText
                    i = i + 1

Dim objXmlNode2中的XML文档作为XmlNode

    <?xml version="1.0" encoding="UTF-8"?>
<Results>
    <Result>
        <CaseEventInternalID>1810457949</CaseEventInternalID>
    </Result>
    <Result>
        <CaseEventInternalID>1810457949</CaseEventInternalID>
    </Result>
</Results>

我的vb代码无效

For Each objXmlNode2 In objXmlResponseDoc2.DocumentElement.SelectNodes("Result")
                        If (objXmlNode2.SelectSingleNode("CaseEventInternalID").InnerText > 1) Then
                            Msc.Integration.Utility.Library.v4.InternalResource.Database.ExecuteScalar("DELETE FROM DocumentPublishingLookup WHERE CaseEventInternalID = " + objXmlNode2.SelectSingleNode("CaseEventInternalID").InnerText, strDPDatabase, strDPServer, strDPUser, strDPPW)
                            'remove from node list
                            objXmlNode2.ParentNode.RemoveChild(objXmlNode2)
                        End If
                    Next

1 个答案:

答案 0 :(得分:0)

如果您乐意使用Linq to XML,那么这是一种简单的方法:

Dim xd = XDocument.Parse(xml)

Dim duplicateIds = _
    xd _
        .Root _
        .Elements("Result") _
        .Elements("CaseEventInternalID") _
        .Select(Function (x) x.Value) _
        .GroupBy(Function (x) x) _
        .Where(Function (x) x.Skip(1).Any()) _
        .SelectMany(Function (x) x.Take(1)) _
        .ToArray()

返回:

{ 1810457949 }