2015-04-19 30 views
1

我怎样才能通过每一父节点的值从第一个开始到最后和应用:VBA开始在第一个节点,如果它存在 - 使用如果没有

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> 

电流输出: “香蕉发现” “香蕉发现” “香蕉发现” “香蕉发现”

回答

2

你只是再次从这里的顶级节点重复搜索...

If XMLFile.SelectSingleNode("/catalog/book/banana") Is Nothing Then 

...所以你总是第一个香蕉点回来。您需要在'n'上操作,而不是'XMLFile':

If n.SelectSingleNode("banana") Is Nothing Then 

请记住,您正在遍历层次结构。

+0

谢谢..这是一个漫长的夜晚:) – NRH

相关问题