multidimensional dictionary

Hi, I have an xml file that needs to be imported. The file has items, and the items have sub-items (these subitems can change, so it must be dynamic).

<items>
    <item>
      <id>4</id>
      <voornaam>name</voornaam>
      <achternaam></achternaam>
      <email>support@mail.nl</email>
      <...>
    </item>
    <item>
      <id>5</id>
      <voornaam>Site</voornaam>
      <achternaam>Administrator</achternaam>
      <email>support@mail.nl</email>
      <...>
    </item>
    <...>
</items>

What I have so far:

  Dim root As XmlNode
  root = xml.DocumentElement
  
  Dim itemsNode As XmlNode
  Dim itemNode As XmlNode
  Dim returnItemNode As XmlNode
  
  Dim val As XmlNode
  Dim key As String
  Dim value As String
  // 'we start with 1 because the first xml element is the <return> element
  For items As Integer = 1 To root.ChildCount-1 
    itemsNode = root.Child(items)

    For item As Integer = 0 To itemsNode.ChildCount-1
      itemNode = itemsNode.Child(item)
      
      For subItem As Integer = 0 To itemNode.ChildCount-1
        if (itemNode.Child(subItem).ChildCount > 0) Then
          key = itemNode.Child(subItem).LocalName
          value = itemNode.Child(subItem).FirstChild.Value
          dim d as new dictionary
          d.Value("key") = key
          d.Value("value") = value
          myglobal.data.Append(d) 
        End
      Next
    Next
  Next

Now my result in myglobal.data is:

dictionary(with key and value)
dictionary(with key and value)
dictionary(with key and value)
...

What I need is an array like this:

item1 
      dictionary(with key and value)
      dictionary(with key and value)
item2
      dictionary(with key and value)
      dictionary(with key and value)

That way I can loop my data and create an insert statement for each item.

Thanks!

[quote=69519:@Yves Hessels]What I need is an array like this:

item1
dictionary(with key and value)
dictionary(with key and value)
item2
dictionary(with key and value)
dictionary(with key and value)
That way I can loop my data and create an insert statement for each item.
[/quote]

Why use an array? Why not a dictionary?

For example, if your property myglobal.data were a dictionary, instead of appending the each sub dictionary to it as you do (myglobal.data.Append(d)), you’d write:

myglobal.data.value(itemNode.name) = d

(This is untested but I think itemNode.name returns the name of the sub-element, such as “item1”, and it would become the key of the outer dictionary.)

This way you can retrieve an dictionary of sub-items via the “item1” key, and then access all of the sub-dictionary’s parts with its keys.

You can also step through the outer dictionary just like an array if you want, via its .keys property.

I do stuff like this all the time. Dictionaries are awesome.

And in memory or on disk database might work better

Why not insert the data in the original loop through the xml document? Why create another tree structure when you already have one?

When I use that, I get this:

item1
  dictionary(with key and value) // only the last sub-item (e-mail in this case)
item2
  dictionary(with key and value) // only the last sub-item (e-mail)

Now the problem remains, but is moved a level down :slight_smile: , it overwrites the element each time so it only saves the last sub-item.

That’s a good alternative, but now I got curious and I would also like to figure it out the other way :wink: But thanks!