Populating properties from an XML Preference file

I have a very simple XML Preferences file, which I read into a property C_PREFERENCES_XML in a module in my application. I am then parsing that XML file into a dictionary, which contains the key/value pairs in the file. All good.

The XML file nodes look like this:

<Preference name="P_LOG_VERBOSITY" value="True"/>
<Preference name="P_REMOTE_SERVER_HEARTBEAT_INTERVAL" value="0"/>
<Preference name="P_REMOTE_SERVER_IP" value="192.168.001.220"/>
<Preference name="P_REMOTE_SERVER_PORT" value="8888"/>
<Preference name="P_LOCAL_SERVER_IP" value="192.168.1.221"/>
<Preference name="P_LOCAL_SERVER_PORT" value="8889"/>

Each of the name items is the same name as one of the properties in the module:

image

What I want to do is move the values from the XML file into the appropriate properties. I think the way to do this is with introspection, but I’m really not clear on how that works for something like this. I’ve read the introspection docs three times, and looked at the example project but I’m not quite getting how I would:

  1. Get the names of all the properties in one module only
  2. use that name to assign a value from the dictionary to the corresponding key (which happens to be the name of the property).

Is this possible? If so, how?

I’m trying to avoid a long switch/case or if/then statement that has to be manually edited each time i add a new preference to the app, which is going to start happening at a rapid pace. The idea is to build something that’s flexible, so that I don’t need to do that.

Introspection doesn’t work on modules, only on classes. If you moved the properties to a class that could work.

e.g.

Dim MySettingsObject As New SettingsObject ' the object to be populated
Dim props() As Introspection.PropertyInfo
props = Introspection.GetType(MySettingsObject).GetProperties()

For i As Integer = 0 To UBound(props)
  If props(i).Name = nameFromXmlFile
    props(i).Value(MySettingsObject) = valueFromXmlFile
  End If
Next

Hmm. That could work. I’ll give it a try tomorrow. Thanks!

So I think redoing all of this to put it into a class is going to be more work than simply hardwiring all the preferences, and remembering to add handler code for each new pref as we add them to the app.

Just to be sure - is there no way for me to obtain a list of properties in a module, for the purpose of populating those properties from a dictionary?