Stumped again – Strongly Typed Datasets from XML Schema

I’ll tell you when I do stuff that’s cool, and when I do stuff that doesn’t quite work at all. Looks like I’m stuck on another issue with .NET. I’ll have to do something cool just to make up for all the bad news lately.

I wanted to see how tough it would be to create use XML as a simply database. I figured the way to start would be to create an XSD schema and use that to generate a data entry form. I don’t care about auto-generating forms, just being able to validate, store, and render more strongly typed and validated data. I used the ‘xsd’ utility to generate a dataset and figured it wouldn’t be tough to bind it to winform controls. Simply value types aren’ tough but ‘xsd’ doesn’t do anything for enumerations. My goal has been to bind an enumerated value to a listbox, showing the string constants but saving the value in the dataset. So for example:

public enum SpecialColors { red=1, blue=2 }

I want the listbox to show red and blue but the data to be stored as 1 or 2. where the constant equals the value, I’d like to store the string equivalent of the constant, "red" or "blue". Doing the conversion isn’t tough, it’s binding that data to the control without a lot of custom code.

I found at least two people expressing the concept. The first link led me to the second:
And here’s another for good measure.

At the second link the guy (Casey) has "Stronger Typed DataSet" code that uses reflection to read a project file and an XSD, generate the missing enums and create a class that extends what’s generated by ‘xsd’. Unfortunately it’s old, needs tweaks for .NET 2, and doesn’t quite go all the way, but it’s a good start. I know the enum isn’t an implementation of ITypedList, so it can’t be used directly for a listbox, but I was looking for something that would connect the dots for me. I’m at a bridge now that I’m not sure I want to cross.

In crossing the bridge, I can encapsulate the enums in the data definition class and throw exceptions if an invalid value is passed in for update. I can create a routine to return a List of strings which can be bound to a listbox. I can probably get this code to do what I want with a bit of code. But unless I enhanced the StrongerTDS for this purpose and published the results, it would be a one-shot effort.

If I decide to turn around and not cross that bridge, I can do this manually and it might even be easier. Rather than trying to use enums from a single class, I can bind controls to individual classes and then validate again against the classes when a row is being updated. Heck, if I hardcode the small enum values into listboxes, rather than using databinding it would only take a couple minutes to get this whole thing done. But that doesn’t help with the next project, and it means I need to remember to change my code in X places if I decide to add another color to my list of supported options.

With all of the excitement about XML interoperability in .NET, code-less drag-n-drop forms that bind to schemas, etc, I was hoping there would be an easy way to do this. As always, tools improve our lives by making 80% of the tasks take 20% of the time they used to, but now the other 20% takes about 80% of our time.

I’ll keep looking around and let you know if anything comes up. Errr, if you happen to have a solution, I’m all ears!

Here’s another article on the topic over at our buddies at C#Station (no mention of enums tho). Here’s a note regarding Microsoft’s position on the topic back in 1995. And here is a Google search on the topic if you’re interested. This is where I’ll be hunting for solutions.

Leave a Reply