Parsing XML without attributes

I had deleted this because I found a solution on my own, but then I got a message from someone who was interested in how I did it, so I’m un-deleting this. Solution posted as a reply.

I have XML files that don’t use attributes. This is out of habit, but also in keeping with W3School’s recommendations.

Almost all of the documentation I see for Xojo XML parsing is based on self-closing tags with the data in attributes. My XML file looks like this:

<Project>
	<!-- GLOBAL PROJECT INFORMATION -->
	<Name></Name>
	<Gauge>0</Gauge>
	<ColorMode>0</ColorMode>
	<ColorDepth>0</ColorDepth>
	<HDRMode>0</HDRMode>
	<ResX>4096</ResX>
	<ResY>3112</ResY>
	<CameraOriginX>0</CameraOriginX>
	<CameraOriginY>0</CameraOriginY>	
	<OutputFileFormat>0</OutputFileFormat>
	
	<!-- CLIP INFORMATION -->
	<ClipList>
		<Clip>
			<ClipName></ClipName>
			<ClipPath></ClipPath>
			<ClipStartFrameNo></ClipStartFrameNo>
			<ClipEndFrameNo></ClipEndFrameNo>	
			<ClipSequencePadding>7</ClipSequencePadding> 
		</Clip>
	</ClipList>
</Project>

In this file, all the data resides between the element tags rather than inside a self-closing element, in keeping with how I was taught to structure XML. I find it to be much easier to read this way, too. In any case, when I read this into an XMLDocument, how do I get the data of each element?

When I set a break in my code and look at the current element as I’m walking through the XML tree, I can only find the data in ToString:

Does this mean I need to parse that to extract the actual data? That seems odd to me.

1 Like

It was kind of counterintuitive, since I tend to think of the stuff between tags as “data” rather than as a “child”. But Xojo treats that data as a child of the element as well, so it puts the data in the FirstChild variable.

In my case, to get the 0 out of <ColorMode>0</ColorMode> I have to first test that we’re in the ColorMode element, then I can get at that 0:

Var root As XmlNode
root = projectFile.DocumentElement  //projectFile is an XMLDocument object I'm passing to this method
Var projectElement As XmlNode

For c As Integer = 0 To root.ChildCount-1
  projectElement = root.Child(c)
  if projectElement.Name = "ColorMode" then
     //This is where the data is:
     var ColorModeData as string = projectElement.FirstChild.Value
  end if
next
2 Likes

Nice it’s here again, because other readers may be also interested in problem & solution.

1 Like

Is not Xojo.
This is how XML is defined
The node contents is a TextNode and its contents is the value