VBA 从第一个节点开始 - 如果它存在 - 使用 If Is Nothing
VBA Start at first node if it exists - using If Is Nothing
如何从第一个到最后一个递增每个父节点并应用:
For Each n In XMLFile.SelectNodes("/catalog/book")
If XMLFile.SelectSingleNode("/catalog/book/banana") Is Nothing Then
MsgBox ("banana not here")
Else
MsgBox ("banana found")
End If
Next
香蕉在第一本书中不存在:
?xml version="1.0"?>
<catalog>
<book id="Adventure">
<author>Ralls, Kim</author>
<title>XML Developer's Guide</title>
<price>44.95</price>
</book>
<book id="Adventure">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<price>5.95</price>
<banana>ring</banana>
</book>
<book id="Adventure">
<author>Ralls, Kim</author>
<title>Mist</title>
<price>15.95</price>
<banana>ring</banana>
</book>
<book id="Mystery">
<author>Ralls, Kim</author>
<title>Some Mystery Book</title>
<price>9.95</price>
<banana>ring</banana>
</book>
</catalog>
当前输出:
"banana found"
"banana found"
"banana found"
"banana found"
您只是从此处的顶级节点再次重复搜索...
If XMLFile.SelectSingleNode("/catalog/book/banana") Is Nothing Then
...所以你总能得到第一个香蕉节点。您需要在 'n' 上进行操作,而不是 'XMLFile':
If n.SelectSingleNode("banana") Is Nothing Then
请记住,您正在遍历层次结构。
如何从第一个到最后一个递增每个父节点并应用:
For Each n In XMLFile.SelectNodes("/catalog/book")
If XMLFile.SelectSingleNode("/catalog/book/banana") Is Nothing Then
MsgBox ("banana not here")
Else
MsgBox ("banana found")
End If
Next
香蕉在第一本书中不存在:
?xml version="1.0"?>
<catalog>
<book id="Adventure">
<author>Ralls, Kim</author>
<title>XML Developer's Guide</title>
<price>44.95</price>
</book>
<book id="Adventure">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<price>5.95</price>
<banana>ring</banana>
</book>
<book id="Adventure">
<author>Ralls, Kim</author>
<title>Mist</title>
<price>15.95</price>
<banana>ring</banana>
</book>
<book id="Mystery">
<author>Ralls, Kim</author>
<title>Some Mystery Book</title>
<price>9.95</price>
<banana>ring</banana>
</book>
</catalog>
当前输出: "banana found" "banana found" "banana found" "banana found"
您只是从此处的顶级节点再次重复搜索...
If XMLFile.SelectSingleNode("/catalog/book/banana") Is Nothing Then
...所以你总能得到第一个香蕉节点。您需要在 'n' 上进行操作,而不是 'XMLFile':
If n.SelectSingleNode("banana") Is Nothing Then
请记住,您正在遍历层次结构。