Xml behaviour I didn't expect

Hello everybody

I’m trying to get a textvalue from a XmlNode. This is a snippet of the xml:

[quote]

If…Then[/quote]

I’m testing:

MsgBox(xmlDoc.Child(0).Child(0).Child(0).Tostring)

Returns: If…Then

MsgBox(xmlDoc.Child(0).Child(0).Child(0).Child(0).ToString)

1 child deeper in the tree.
Result: An empty string!

Why isn’t the output “If…Then”?
What am I doing wrong?

“If…Then” is not a child of the child of the XMLTextNode, it is its value:

MsgBox(xmlDoc.Child(0).Child(0).Child(0).Value)

Actually, I believe he needs the fourth “child(0)” to get the text element.

xmldoc.child(0).child(0).child(0).child(0).value

[quote=68556:@Peter Truskier]“If…Then” is not a child of the child of the XMLTextNode, it is its value:

MsgBox(xmlDoc.Child(0).Child(0).Child(0).Value)

Already tried that, returns empty string. But thanks :slight_smile:

[quote=68558:@Bill Gookin]Actually, I believe he needs the fourth “child(0)” to get the text element.

xmldoc.child(0).child(0).child(0).child(0).value

You’re right, that did the trick, didn’t thought of this, because getting a value from an empty string looked weird to me.

Thanks! Fast replies at this forum!

Whoops. Sorry; I copied the wrong one to modify…

No problem Mr Truskier, you don’t have to apologize for trying to help me and making a typo!

This is a common misunderstanding of the structure of XML and is not unique to Xojo. The xml snippet

If…Then

looks like one node with a value of “If…Then”, but in fact it is 2 nodes: a node named “name” that has a child node (a text node) with no name and a value of “If…Then”. The reason for this extra node is that can be

If…Then

or id could be

<name>
     <firstname>Joe</firstname>
     <lastname>Schmeckman</lastname>
     <address>
           <street>123 Main St</street>
           <city>Dallas</city>
           <state>TX</state>
     </address>
</name>

The node must be consistent either way, regardless of how complex its children may be. In your mind, convert the shorthand (human readable) notation of <name>If..Then</name> to the way the machine reads it:

<name>
     If..Then
</name>

Thanks Mr Hare, your explanation clears things up :slight_smile: