XML GetAttribute failure?

Has anyone else seen this? Maybe I’ve missed a bug report somewhere.

I have XML that looks like this:

<cell tag="something" data_value="something_else" group_index="1" gotValue="" row_index="1"/> <cell tag="another_thing" data_value="another_something_else" group_index="1" gotValue="" row_index="1"/>

When I walk through this XML with an XMLNodeList, I can get each node fine. However, when I come to this statement:

dim myNode as XMLNode = nodelist.item(i) dim myTag as string = myNode.GetAttribute("tag")

myTag is always returned as an empty string!
I’m watching this in the debugger; I can see the node, and myNode.ToString() shows me that the attribute is there. But GetAttribute doesn’t get it.

I’ve also tried using GetAttributeNode, which returns Nil. On the off chance that an attribute name of “tag” is somehow reserved, I changed it, but no joy.

I should note that this is happening inside an object that’s running in a XojoScript context. If I take the exact same code and run it without being called from XojoScript, it works as expected.

And I know that XojoScript doesn’t support objects; this code is inside an object that’s part of the XojoScript context.

What the @#$! is going on here? Any help is appreciated.

Thanks,
Chuck

Here’s an additional data point:

If I do this:

dim foo as XMLNode = myNode.clone(true)

Then look at both myNode and foo in the debugger, it says that the AttributeCount for myNode is 6, but the AttributeCount for foo is 4. If I clone a node, shouldn’t it, umm, clone its attributes as well?

The plot thickens…

Another update: don’t bother looking at this right now. I can’t reproduce it in a simplified test project.

OK, nobody commented on this so I guess it only affects me.

I’ve found a workaround, which is to create a brand-new XMLDocument object EACH TIME I need to access an attribute in a node.

Then GetAttribute works as expected.

I have no idea why this is happening, but it’s truly annoying.

Are there XML namespaces involved? What does myNode.ToString produce?

Good suggestion Mikko, but no namespaces were used.

myNode.ToString produces exactly what I’d expect, i.e.

<cell tag="something" data_value="something_else" group_index="1" gotValue="" row_index="1"/>

I’ve replicated this problem in a small example project, and filed bug ID 39817

Sorry to dig this old thread, but it looks like it is still an issue:

Dim node As XmlNode
Dim nodes As XmlNodeList
Dim xml As New XmlDocument

xml.LoadXml( file )
nodes = xml.XQL( "//item" ) 

For i As Integer = 0 To nodes.Length - 1
  node = nodes.Item( i )
  
  Dim id As Integer = Val( node.GetAttribute( "id" ))
  Dim name As String = node.GetAttribute( "id" )
Next i

id contains 1, 2, 3 etc. as expected, but name always returns an empty string (note it is the same attribute).

Not much to go on there James except “its not working”
This works

dim xmldoc as string = "<?xml version=""1.0"" encoding=""UTF-8""?>" + _
"<League>" + _
"<Team name=""Seagulls"" id=""1"">" + _
"<Player name=""Bob"" position=""1B"" />" + _
"<Player name=""Tom"" position=""2B"" />" + _
"</Team>" + _
"<Team name=""Pigeons"" id = ""2"">" + _
"<Player name=""Bill"" position=""1B"" />" + _
"<Player name=""Tim"" position=""2B"" />" + _
"</Team>" + _
"<Team name=""Crows"" id=""3"">" + _
"<Player name=""Ben"" position=""1B"" />" + _
"<Player name=""Ty"" position=""2B"" />" + _
"</Team>" + _
"</League>"

Dim node As XmlNode
Dim nodes As XmlNodeList
Dim xml As New XmlDocument( xmldoc )

nodes = xml.XQL( "//Team" ) 

For i As Integer = 0 To nodes.Length - 1
  node = nodes.Item( i )
  
  Dim id As Integer = Val( node.GetAttribute( "id" ))
  Dim name As String = node.GetAttribute( "id" )
  
Next i

I use extensively XML docs and I have not problem in reading the attributes.

Without any clue about the XML document I can’t give any help.