RSS
 

Awesome Object to XML conversion

03 Aug
Microsoft are taking their sweet time releasing a production version of LINQ to XSD. I have played with both of the alphas, and they are seriously the most perfect way to deal with strongly typed XML. I hope you've played with the new System.Linq.XML bits, they make xml operations a lot easier. However, when you've got an XSD file, you know exactly what kinds of elements and attributes you should be generating, and it seems like hardcoding in those strings is error-prone. Back in .net 2, I used to use xsd.exe to generate a class that was in effect a strongly typed map to the desired XML format. Then it's totally easy to convert from that object to XML and back. Now, in .net 3.5, you can use vanilla LINQ to populate these data classes, which is certainly nicer to read. But it feels somehow low tech. Plus it feels cumbersome to have to generate the classes from the command prompt, I want visual studio to mollycoddle me (and stop anyone from making changes to the generated file in case I want to regenerate!) The promise of LINQ to XSD, however, is slightly different. And awesome. You put your XSD file into your project. Change the build action (or maybe it's the custom tool, it's been a while since I checked). After that, as a precompile step in your build, visual studio will regenerate a set of classes that strongly type your XSD. What's so cool about that (apart from the fact that you don't have to drop to the command prompt?) Well now the new classes are already backed (ie, keep all their data in) by System.Linq.XML.XElements - no more mucking about with the XMLSerializer. And for two, the classes have been infused with some validation, so if you try to set a property with a value that the schema considers invalid, you'll get an error immediately (ie: you can see which line of code caused the problem). Awesome. For further reading, check out what Scott Hanselman has to say about the "life" of the project. He explains Linq to XSD pretty well too. And if you get a chance, make some noise, I'd really like to see this released!
 
No Comments

Posted in .Net

 

Tags: ,

Leave a Reply