I’m trying to read a rather large XML file with tons of fields.
I can read the file properly but I’m stuck finding the right way to store and then later work with the key/values.
To keep it simple, let’s say the structure is something like a collection of persons:
name - value
address - value
zip - value
city - value
country - value
phone - value
However, instead of 6, there are 50+ fields and 20 or 30 ‘persons’ per file.
Because there are so many fields, I would like to use a dictionary and instead of setting the key to ‘John Smith’, i prefer to set it to ‘name’ (the XML node name). That’s fine with one person but I don’t see how that’s supposed to work with multiple.
So on to a class. With a class, I can do appends but I also need to pre-define all fields.
2 questions:
When going with a dictionary, what would the best way be? Multidimensional? How does that work? Or putting them in an array (if possible) where the key is some kind of ID? But if I do that, I think I lose the benefit of dictionaries and their fast lookups.
With a Class where all the properties are pre-defined, is it possible to somehow dynamically fill in the property name when assigning values to it? (something like myClass.xmlNodeName where xmlNodeName is a variable?) If that’s not possible, how’s that normally done? Going through a large Select Case when parsing the XML?
This is most likely a ver basic question but I’m trying to learn. A pointer in the right direction would be very welcome.
What about a class that internally just uses a dictionary
And each instance represents one Person
That way you can have an array of these and they can, on a per person basis, have differing numbers of “fields” each accessed by the name of the field (from the XML)
the only difficulty with creating a class is, without a priori knowledge of what fields ARE in every xml file for each person, you might need to be extra flexible & handle them in whatever form they come in
I’d agree that NORMALLY you want to have a representation that would map to a class or that you can represent in a sql table / db
sometimes you do need the flexibility of a class that has an indefinite list of “properties” that you can access “by name”
operator_lookup is really handy for this kind of case
I never heard of operator_lookup before. Very interesting.
In this case, the fields are known. It’s just more work to map things correctly. The Dictionary approach looked so much simpler and cleaner.
Thanks!
Interesting, yes, but I’d caution against it. The compiler can’t help you if you use the ‘wrong’ name because the member lookup just turns into a call to Operator_Lookup at runtime.
If you know the structure of the XML file and it is well defined, create a class to represent your data. Only use a dictionary if you are working with a structure that changes frequently and you only need access to a few fields that you know are “stable” in their definition. Creating a class gives you all the benefit of the compiler and IDE. For example, when using a dictionary you could do:
Print dict.Value("FirstName")
// Later in Code
dict.Value("FirsName") = "Jim"
Notice the slight typo. If however you had a class, you would get IDE completion of FirstName and if you did misspell it a compiler error. Further, you will get real types. For example, FirstName as String, BirthDate as Date, Age as Integer, etc…
It’s a little work at the start but I believe the benefits far outweigh the work, especially if this is an app that is going to stay around for a while. Maybe a one time data conversion or something I wouldn’t go through the trouble, but anything more than that… go through the trouble.