I have a problem with the XmlNodeList.XQL method on both RS 2012r2.1 and Xojo 2013r2
To make an example, imagine that I have this XML code:
<movies>
<movie>
<title>
Casablanca
</title>
</movie>
<movie>
<title>
Gone with the wind
</title>
</movie>
</movies>
I’d like to get the title of the first movie. So I decide to query the XML code to get the first node in by using the XQL method; then, I’d like to take that first element and make another query on it to get the first node named that appears inside of it.
When I do this, the first query correctly gives me back this list:
1) <movie><title>Casablanca</title></movie>
2) <movie><title>Gone with the wind</title></movie>
Then, I take the first item from that list, that is:
<movie><title>Casablanca</title></movie>
And I make another query on it to get a list of every element named that are nested inside. I’d expect to get just one item, but instead I’m getting two of them:
1) <title>Casablanca</title>
2) <title>Gone with the wind</title>
The first item is as expected (Casablanca), but the second item (Gone with the wind) doesn’t belong to the node I just queried.
Since I’m doing a query on a specific node and not on the entire node, I wasn’t expecting it.
It’s driving me mad, so I really need to ask you the favor of telling me if you see any flaw in the source code I wrote below.
Is this a bug? Or do I have written something wrong?
This is the complete source code I wrote:
Dim source As String = "<movies>" _
+ "<movie><title>Casablanca</title></movie>" _
+ "<movie><title>Gone with the wind</title></movie>" _
+ "</movies>"
Dim xmlSource As XmlDocument = New XmlDocument()
xmlSource.LoadXml(source)
Dim movieList As XmlNodeList = xmlSource.Xql("//movie")
Dim firstMovie As XmlNode = movieList.Item(0)
' firstMovie contains:
'
' <movie><title>Casablanca</title></movie>
' Now, the following line is important:
'
' -----------------------------------------------
Dim theTitleElement As XmlNodeList = firstMovie.Xql("//title")
' -----------------------------------------------
'
Dim title As XmlNode
Dim output As String = ""
Dim nInd As Integer = 0
While (nInd < theTitleElement.Length)
title = theTitleElement.Item(nInd)
output = output + title.ToString + EndOfLine
nInd = nInd + 1
Wend
MsgBox(output)
' This outputs:
'
' <title>Casablanca</title>
' <title>Gone with the wind</title>