XMLSerialization absolutely rocks. I am blown away by the flexibility and richness it provides and how easy it is to use…once you get past the initial learning curve.
I implemented it for a tool I’m writing that allows me to control Windows Media Center Edition remotely via a TCP based protocol. My home AV control system uses Crestron and I need to be able to have Crestron be able to control MCE. For various reasons I can’t use IR for this, so I want to do it over Ethernet.
So I built an app in C# that listens on a TCP socket for text commands and does the appropriate SendInput, SendMessage, and “create process” actions required to control MCE. For example the command “up” results in a SendInput call with the VK code for Up. “mymusic” results in a Ctrl-I, which is the shortcut for MyMusic in MCE.
As I built this thing I realized that I was basically re-writing Girder. I have been using Girder to solve this problem for a while, but it’s TCP support requires an MD5 hash for authn and that is causing me headaches. Instead of trying to fix Girder’s TCP support I decided to write this tool (I needed an excuse to do more C# coding!)
There are roughly 45 commands that MCE exposes, and a few more that other apps I want to control need. So I wanted an extensible way of adding/modifiying commands. Initially I got the app working by hard coding the commands into it. I have a class “Command” with sub-classes for each of the types of commands and I put these in a HashTable based on the commandname I defined.
Once I had this working, I wrote a “Serialize” method and started playing with XMLSerialization to cause my hard-coded command entries to be written to an XML file. After getting over things like XMLSerialization not supporting IDictionary objects (I had to “wrap” my HashTable), XMLArrayAttributes, and so forth I was able to tweak the output so that XML file was something that I (and others who use this app once I release it into the wild) could easily edit to extend/update. This involved changing the default behavior for some members to use attributes instead of elements etc…
Once I had this working , 4 lines of code in a “Deserialize” method I wrote WORKED THE FIRST TIME to read the latest XML I had generated from my hard-coded entries. I simply commented them out and now I can use the XML file to change my config!
Very, very cool.