2015-04-19 33 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

相關問題