【问题标题】:Compare 2 xmlNodeList to find difference比较 2 xmlNodeList 以发现差异
【发布时间】:2019-08-12 20:54:24
【问题描述】:

我从同一个 xmlDocument 中提取了两个 xmlNodeList。 第一个 allNodes 包含文档中的所有 MACHINECONFIGURATIONID 节点,另一个 numberedNodes 包含具有 NUMBER 个子节点的所有 MACHINECONFIGURATIONID 节点。

我想创建第三个 xmlNodeList,nonNumberedNodes,其中包含 allNodes 中不在 numberedNodes 中的节点。

如果这些是(XmlNode 的)列表,我会这样做:

nonNumberedNodes = allNodes.except(numberedNodes)

但这不适用于 xmlNodeList

这是我目前的代码

Sub stripNodesWithoutNumbers()

        Dim xmlFile As New XmlDocument
        Dim xmlFileArray() As String
        Dim numberedNodes As XmlNodeList
        Dim allNodes As XmlNodeList

        Dim x As List(Of XmlNode)

        Dim nonNumberedNodes As XmlNodeList

        xmlFileArray = Directory.GetFiles(TextBox1.Text, "*.xml")

        For Each file In xmlFileArray

            xmlFile.Load(file)

            numberedNodes = xmlFile.SelectNodes("/MACHINECONFIGURATIONVALUES/MACHINECONFIGURATIONID[../NUMBER]")

            allNodes = xmlFile.SelectNodes("/MACHINECONFIGURATIONVALUES/MACHINECONFIGURATIONID")


        Next

    End Sub

【问题讨论】:

    标签: xml vb.net


    【解决方案1】:

    您可以将这些 XmlNodeList 转换为 List(of XmlNode) 然后使用 except

    Sub stripNodesWithoutNumbers()
        Dim xmlFile As New XmlDocument
        Dim xmlFileArray() As String
        Dim numberedNodes As List(Of XmlNode)
        Dim allNodes As List(Of XmlNode)
    
        Dim nonNumberedNodes As List(Of XmlNode)
    
        xmlFileArray = IO.Directory.GetFiles(TextBox1.Text, "*.xml")
    
        For Each file In xmlFileArray
            xmlFile.Load(file)
    
            'Cast to XmlNode to allow enumeration to convert to list
            numberedNodes = xmlFile.SelectNodes("/MACHINECONFIGURATIONVALUES/MACHINECONFIGURATIONID[../NUMBER]").Cast(Of XmlNode).ToList()
    
            allNodes = xmlFile.SelectNodes("/MACHINECONFIGURATIONVALUES/MACHINECONFIGURATIONID").Cast(Of XmlNode).ToList()
    
            nonNumberedNodes = allNodes.Except(numberedNodes).ToList()
        Next
    End Sub
    

    然后,如果您需要生成的nonNumberedNodes 作为XmlNodeList,您可以创建一个新的XmlDocument 并从中选择:

    Dim resultDoc As XmlDocument = New XmlDocument()
    resultDoc.AppendChild(resultDoc.CreateElement("MACHINES"))
    For Each nd In nonNumberedNodes
        resultDoc.DocumentElement.AppendChild(resultDoc.ImportNode(nd, True))
    Next
    
    Dim result As XmlNodeList = resultDoc.SelectNodes("/MACHINECONFIGURATIONID")
    

    【讨论】:

      【解决方案2】:

      我找到了一种更优雅的基于 Xpath 的方法。

      nonNumberedNodes = xmlFile.SelectNodes("/MACHINECONFIGURATIONVALUES/MACHINECONFIGURATIONID[not (NUMBER)]")
      

      这条线将我直接带到我想要的列表,所有没有数字节点的 MACHINECONFIGURATIONID 节点。因此无需比较列表或遍历每个节点,这是性能杀手。

      Sub stripNodesWithoutNumbers()
      
              Dim xmlFile As New XmlDocument
              Dim xmlFileArray() As String
      
              Dim nonNumberedNodes As XmlNodeList
      
              xmlFileArray = Directory.GetFiles(TextBox1.Text, "*.xml")
      
              For Each file In xmlFileArray
      
                  xmlFile.Load(file)
      
                  nonNumberedNodes = xmlFile.SelectNodes("/MACHINECONFIGURATIONVALUES/MACHINECONFIGURATIONID[not (NUMBER)]")
      
                  For Each nonNumberedNode In nonNumberedNodes
      
                      nonNumberedNode.ParentNode.RemoveChild(nonNumberedNode)
      
                  Next
      
                  xmlFile.Save(file)
      
              Next
      
          End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 2010-10-31
        • 2014-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多