How do you store Dictionaries in a Dictionary

Given the following xml…

<?xml version="1.0" encoding="UTF-8"?> <schools> <details> <name>Academy of the Pacific</name> <id>1</id> <districtid>1</districtid> <principal>Jim Johnson</principal > </details> <details> <name>Ahuimanu Elementary</name> <id>107</id> <districtid>6</districtid> < principal >Mary Bell</principal > </details> </schools> ...........[

I am trying to save this in a dictionary by storing the details information in a dictionary then storing the details dictionary into a schools dictionary. In the Characters event handler I am filling the details dictionary then in the EndElement event handler I am adding the details dictionary to the schools dictionary. So it looks something like this…

Characters event handler:


EndElement event handler


That works except that the when complete every school has the details of the last school processed. I think I understand why… I am putting the same detailsDictionary instance in every school. Maybe somebody can explain the why better to me, but the how is my question. How can I store different dictionaries into the schools dictionary?



Whiteout seing the code it is difficult to say. My guess is, that you only do process EndElement event handler once at the end of all detail entries instead of doing it after each details Dictionary is filled.

You need to instantiate a different detailsdictionary for each schoolsdictionary entry.

Else you end up with exactly what you describe: You only have one detailsdictionary and assign different entries to this, then attach it to the schoolsdictionary.

Your code should therefore look a bit like:

[quote=202882:@John Baughman]Characters event handler:
Dim schoolDetailsDictionary as new dictionary

[quote=202882:@John Baughman]EndElement event handler
(or a bit different depending on what the detailsdictvalue"Id" means and if you need to have a global detailsdict anyway. And of course you need to retrieve the dictionary in the second event.)

@Ulrich Bogun

Thanks Ulrich. I got it working now. Your answer was where I was going and kept me moviing in the right direction, but I was having trouble with scope, as the schoolDetailsDictionary cannot be instantiated in characters like you suggested. The result would be a dictionary for each detail item.

The schoolDetailsDictionary has to be instantiated before Characters starts firing. Doing so I ran into scope errors until I added the schoolsDetailsDictionary to my XMLReader subclass and assigned a new dictionary to it in the Start Element event handler.

Working solution looks like this…

[code] //StartElement event handler
Key = name //Key is a string property of my XMLReader subclass
If Key = “details” Then
Dim d As New Dictionary
schoolsDetailDictionary = d //schoolsDetailDictionary is a dictionary property of my XMLReader subclass

End If[/code]

  [code]  //Characters event handler
  If Key <> "schools" And Key <> "details" Then
  End If[/code]

//EndElement event handler If name = "details" Then output.Value(schoolsDetailDictionary.Value("id"))= schoolsDetailDictionary //output is a dictionary property of my XMLReader subclass instantiated by the calling method End If


There is a contradiction between these two things you said:

Oh, ok. that should have read “The result would be a dictionary for each details list”, not item. Sorry if I mislead you.


Yes, sorry for a too short replay, John. That’s what I meant with “retrieving the dict” – if you would stick to the event structure, you’d probably have to use a “If Schoolsdetaildictionary = NIL then Schoolsdetaildcitionary = New Dictionary else Schoolsdetaildictionary = Schooldictionary.value(“id”)”, attach it to the parent dict in the first event and modify it on further calls.
But that’s purely theoretical. Glad you made it work!