在添加另一个节点之前,您如何知道节点是否已存在于xml中?

时间:2017-11-28 21:10:22

标签: xml excel vba

从我的Excel工作簿中的vba,我可以多次执行以下操作:

ActiveWorkbook.CustomXMLParts.Add("<authors><author>Christie</author><author>King</author></authors>")

每次我都会创建另一个CustomXMLPart,它们都是相同的(除了自动创建的标准!!)。

我的第一个问题是,在我尝试再次添加她之前,如何确定作者Christie是否已经在列表中?

其次,如何将item与命名参考一起使用?

ActiveWorkbook.CustomXMLParts.Item({named reference})

我在该项目中引用的名称是什么?

由于

1 个答案:

答案 0 :(得分:0)

请看下面的代码段

ActiveWorkbook.CustomXMLParts.Add ("<authors><author>Christie</author><author>King</author></authors>")
For Each xmlCustom In ActiveWorkbook.CustomXMLParts
    Set nodeCheck = xmlCustom.SelectNodes("//author[text()='Christie']")
    If nodeCheck.Count > 0 Then
        MsgBox "already exist in collection"
        Exit For
    End If
Next

For i = 4 To ActiveWorkbook.CustomXMLParts.Count '1-3 are default xmlparts
    Set nodeCheck = ActiveWorkbook.CustomXMLParts.Item(i).SelectNodes("//author[text()='King']")
    If nodeCheck.Count > 0 Then
        MsgBox "already exist in items"
        Exit For
    End If
Next

在第一部分中,我使用for-each完成了集合,这消除了了解集合的上/下边界的痛苦。第二部分使用迭代技术使用集合的每个item

相关问题