2

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.

Pro-grammar
  • 365
  • 2
  • 17
  • 1
    I think this [dynamic version](http://stackoverflow.com/questions/13704752/deserialize-xml-to-object-using-dynamic) is more readable – Eser Sep 30 '15 at 22:38

4 Answers4

4

There's probably nothing inherently wrong with this approach (though there are some pitfalls (see below)), but the main question I have is why?

This is a common problem that a lot of people smarter than either of us have encountered and come up with solutions for; why not use their solutions that have been battle-tested?

Couple of reasons why you don't want to do this:

  1. You can't query your data
    • This data is more of a natural fit for JSON than XML, but disregard that -- what happens when you want to query based on an property (or really, something other than the element name)
  2. You lose the benefit of strongly typed properties
    • It looks like you're just making children Dictionaries whose keys are the index in the collection...is that easier than using XPathNavigator and iterating through the children of a selected node?

I definitely see the value in writing something like this to gain experience as a beginning programmer, but this doesn't belong in prod code -- there are just too many potential issues and not enough actual functionality.

I'll give you some advice from my experience as a programmer: for a very long time you'll want to reinvent the wheel because you can...and that's fine to learn things (I was stuck here for a while). Just be sure to not fool yourself into thinking you're building skyscrapers when you're really putting together a tent (not to say that's what's going on here; just general advice).

funbrigade
  • 321
  • 2
  • 7
  • I'm curious what your thoughts are with examples of a 'potential' issue and 'enough'. These terms are ambiguous to me, for example what qualification does 'enough' have? Potential issues is that a gut feeling, as in because you've seen code like this before which creates issues? I appreciate your initial post and hopefully your response :) – Pro-grammar Oct 01 '15 at 02:18
  • Potential issues would include any bugs that arise from using your class that you can't google as a result of trying to tack on additional functionality. So this is all based on the assumption that you want to add additional features. What would they be? Searching the DOM? Well, that means you should have used an existing library. Maybe figuring out if you have a text or element node? Accessing an attribute? Any of these additions should set off an alarm that says "I'm in over my head" and prompt you to use a full library...that being said, this is a great exercise! – funbrigade Oct 01 '15 at 02:31
  • I've actually already solved all those issues. They were pretty simple to solve. You're right about there not being literature on those issues for this case, I had to come up with the solution that was needed when the issue arose. I hope you don't limit yourself by thinking there is always someone smarter than yourself ;) sometimes there isn't, not because the smart ones couldn't understand it, but because they may have not traveled down a particular train of thought. Thank you for elaborating, I have a better elucidation of what you were getting at :) cheers! – Pro-grammar Oct 01 '15 at 02:49
  • And I'll keep my eye out for getting to in over my head. For now this is more just an experiment or like you said an exercise :D – Pro-grammar Oct 01 '15 at 02:51
2

Your data structure can't handle mixed content and it doesn't retain the order of elements. That makes it suitable for some XML documents (roughly, those which could equally well be handled in JSON) and totally unsuited for others.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

XPath is most likely slower (because expressions need to be compiled) than your recursive dictionary.

However, I recommend using Linq to XML. The code is already written and debugged and has similar access pattern. See How to get value of child node from XDocument

Community
  • 1
  • 1
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
1

The disadvantages I can think of are:

  • In my eyes this is really unreadable having multiple [..] one after each other. You can loose track rather easily, especially if the project/xml becomes bigger.
  • When creating a dictionary like this probably consumes a lot of memory. So you could probably safe some space if you wouldn't load the whole xml into a structure like that. Probably the usage of using helps to cut down this problem.

On the other hand: XPath will probably be slower and maybe a bit overkill if the xml really stays that small.

Either way will work. Decide yourself what fits best.

SkryptX
  • 813
  • 1
  • 9
  • 24