I load an xml file into a recursive dictionary so that I can access xml files in the following way: Example.xml:
<objects>
<object>
<id>256</id>
<objectType>Person</objectType>
<name>Bob</name>
<object>
<id>128</id>
<objectType>BodyType</objectType>
<shape>Athletic</shape>
</object>
<object>
<id>1024</id>
<objectType>Body-Measurements</objectType>
<height>5'9"</height>
<weight>155</weight>
</object>
</object>
<object>
<id>512</id>
<objectType>T-Shirt</objectType>
<object>
<id>64</id>
<objectType>Logo</objectType>
<design>Dragon-Tatoo</design>
<object>
<id>64</id>
<objectType>Design-Color</objectType>
<color>black</color>
</object>
</object>
</object>
Example C# code for using Recursive Dictionary:
RecursiveDictionary RE = loadXML("Example.xml");
Console.WriteLine( ToInt(RE["objects"]["0"]["object"]["id"]) . "\n" );
Console.WriteLine( RE["objects"]["0"]["object"]["0"]["object"]["1"]["height"] . "\n" );
Console.WriteLine( ToConsoleColor(RE["objects"]["1"]["object"]["0"]["object"]["0"]["object"]["color"]).ToString() . "\n" );
Example Output:
128
5'9"
black
The ToConsoleColor()
is NOT need to print out the string 'black' but in my real app I would do the conversion and then set the consoles color to the enum value of what ToConsoleColor()
returns. In this case I'm just printing the ToString() to show kind of the effect I am going for of pulling in an XML in a recursive dictionary and then accessing particulars of the xml file and converting them into useful program data types (in this case a console enum value).
I have code that checks if a key/value or tag/value exists before trying to convert anything, and will print out errors letting me know I didn't process the particular xml tags and for what reason. The code runs as best as it is able to with error xml or not.
I would like to know what the disadvantages of doing it this way are as oppose to using X-Paths.