I’ve been looking at XML serialization / deserialization in VS.Net, and I’m really beginning to like working with the XSD.exe tool that ships with VS.Net. I’ve found that this is a great way to work from example data and end up with code that can serialize and deserialize directly from that data structure.The XSD.exe tool is found in the Visual Studio install directory in the SDK folder, and it’s a command-line tool. It lets you take an XML document and generate an XSD (XML schema) document from it. You can then run XSD.exe again and generate code that works with XML that conforms to that XSD. Of course, you can perform one or the other of these steps if you want (skip generating the XSD if you already have one, for instance).
When I started playing with this tool, I knew very little about XSD schema documents, but the prototyping I’ve been doing has gotten me up to speed pretty quickly, and I think I’m sold on them now. These docs do an amazing job of defining the “low-hanging” business rules that seem to trip up so many applications. The final proof for me will be to see if I can use these without imparting a “sorry, try again” feel to an application (I’d like to guide users, not slap them down).
So far, I’ve found that I’ve been bouncing back and forth from XML to XSD to code trying to get things sorted out the way I want. In my case, I control the XML and the XSD, so I’m adjusting both of them to get a combination I like. It’s also been very helpful to load the XML up with the generated code and browse the object structure at runtime. It’s helped me see a few structural things that I’ve been able to clean up. Working through all of this, there are a lot of features in XSD that really help tighten up the definition of the XML, and being able to see the serializable code and browse the runtime objects really helps my understanding of the overall project.
As with any such generation technology, there’s a round-trip problem here, so I expect that this is mainly a one-time prototype / jump-start tool. I also found it pretty awkward to use VS.Net to look as XSD files, because it launches a designer that doesn’t really give you access to the XSD itself. Despite these small problems, it’s definitely worth a look if you haven’t seen this before!
Learn about XSD on w3schools.com
Online XSD schema validator
MSDN documentation for Xsd.exe
Appdev: Generate web service stubs from WSDL
One Reply to “Generate code from XML”
You guys do a wonderful job! Keep up the good work!!!T
Comments are closed.