trying to parse xml...

<Configuration> <web port="80" /> <System name="Main System"> <ImportServer name="ImportServer" host="importserver.com" port="1104" /> <ExportServicer name="ExportServicer" host="exportserver.com" port="1443" /> </System> </Configuration>
This xml is stored in test.xml on my desktop. I validated the xml with w3school xml validator and it passes.

[code]Sub Open()
dim f as FolderItem = new FolderItem("/Users/bobrien/Desktop/test.xml", FolderItem.PathTypeShell)
dim x as new XmlDocument
x.LoadXml(f)

// check first node of document:
Dim n As XmlNode = x.FirstChild
System.DebugLog(n.Name) // doc node

// and now walk over all sub nodes:
Dim childNode As XmlNode = n.FirstChild
While childNode <> Nil
System.DebugLog("ChildNode.Name = " + childNode.Name)
childNode = childNode.NextSibling
Wend
System.DebugLog(“Done”)
End Sub[/code]
The log shows this:

[quote]2013-07-20 9:36:51.334 PM My Application.debug[67070]: Configuration
2013-07-20 9:36:51.334 PM My Application.debug[67070]: ChildNode.Name = web
2013-07-20 9:36:51.338 PM My Application.debug[67070]: ChildNode.Name = System
2013-07-20 9:36:51.338 PM My Application.debug[67070]: Done
[/quote]
I’m confused by how his is working.
I would have thought there would have been some mention of ImportService and ExportService in the log…
Am I parsing this wrong?

ImportServer and ExportServicer are both children of the System node.

Do you have a better method, (preferably not a recursive one, but that may be the only way) to traverse the xml?

I don’t have time to whip one up now, and recursive would be easiest, but you can maintain your own stack in an array. Start at the first child, process it, then check its ChildCount. If it’s <> 0, push that node onto your stack, then set the current node to the child. As you go through the loop, use NextSibling to get the next node to process (and check its children). When NextSibling returns nil, pull the last value off the array and get NextSibling of that. When NextSibling is nil and the array is empty, you’re done.

Or something like that.

Thanks, I understand if you don’t have time…

If i understand the structure then
web and system are children of configuration
web and system are siblings.
import and export are children of system.
What then are name, host and port?

Attributes.

This:

[code] Dim x As New XmlDocument

Dim f As FolderItem = New FolderItem(“C:\Debug\test.xml”, FolderItem.PathTypeShell)

x.LoadXml(f)

Dim xRoot As XmlNode
Dim xNode As XmlNode
Dim xChild As XmlNode
Dim xAtt As XmlAttribute

xRoot = x.FirstChild
me.Text = xRoot.Name + EndOfLine

For i As Integer = 0 to xRoot.ChildCount - 1
xNode = xRoot.Child(i)
me.Text = me.Text + "Child Name : " + xNode.Name + EndOfLine
For j As Integer = 0 To xNode.ChildCount - 1
xChild = xNode.Child(j)
me.Text = me.Text + "Grandchild Name : " + xChild.Name + EndOfLine
For k As Integer = 0 To xChild.AttributeCount - 1
xAtt = xChild.GetAttributeNode(k)
me.Text = me.Text + "Attribute Name : " + xAtt.Name + " Value : " + xAtt.Value + EndOfLine
Next k
Next j
Next i

me.Text = me.Text + “Done”[/code]

Gives this:
Configuration
Child Name : web
Child Name : System
Grandchild Name : ImportServer
Attribute Name : name Value : ImportServer
Attribute Name : host Value : importserver.com
Attribute Name : port Value : 1104
Grandchild Name : ExportServicer
Attribute Name : name Value : ExportServicer
Attribute Name : host Value : exportserver.com
Attribute Name : port Value : 1443
Done

Where me is a text area

That’s a fine solution if you know that the XML will only be two levels deep, and attributes will only be on the second level. Otherwise, you have to walk through each node.

God bless recursion!

[code]Sub Open()
dim f as FolderItem = new FolderItem("/Users/bobrien/Desktop/test.xml", FolderItem.PathTypeShell)
dim x as new XmlDocument
x.LoadXml(f)

DumpXml(x.FirstChild, 0)

End Sub

Private Sub DumpXml(n as XmlNode, level as integer=0)
Dim xAtt As XmlAttribute
dim ts, ts1 as string
dim i as integer

for i=0 to level
ts = ts + chr(9)
next
ts1 = ts + chr(9)

system.DebugLog(ts+n.Name)

for i=0 to n.AttributeCount-1
xAtt = n.GetAttributeNode(i)
System.DebugLog(ts1 + xAtt.Name + “=” + xAtt.Value)
next

for i=0 to n.ChildCount-1
DumpXml(n.Child(i), level+1)
next

End Sub
[/code]

I put some tabs in to show the depth of the child, but it’s not quite right… but it seems to traverse everything…

This works… thanks for your interest and help.

[code]Private Sub DumpXml(n as XmlNode, level as integer)
Dim xAtt As XmlAttribute
dim ts, ts1 as string
dim i as integer

for i=0 to level-1
ts = ts + " "
next
ts1 = ts + " "

system.DebugLog(ts+"<"+n.Name+">")

for i=0 to n.AttributeCount-1
xAtt = n.GetAttributeNode(i)
System.DebugLog(ts1 + xAtt.Name + “=” + xAtt.Value)
next

for i=0 to n.ChildCount-1
DumpXml(n.Child(i), level+1)
next

system.DebugLog(ts+"</"+n.Name+">")

End Sub
[/code]