I am looking to create a simple code generator to convert XSD definitions to C# classes. This is partly as a learning exercise, but I would also like to get some practical use out of it eventually.
What would the general design of such an application look like? I assume that I will need to use one of the XML parsers in the .NET framework, but which one? And what will the resulting data structure look like? Will it be an expression tree?
From there, what happens? Do I generate code by concatenating strings, or are there better, more sophisticated ways to do it?
Important: I'm not looking for someone to write code for me (although very brief code samples are welcome, if they are applicable), and I don't need someone to do the research for me. I'm perfectly capable of reading about parsers, expression trees and code generators myself. I just need someone to help me with the broad brush strokes, so that I don't walk into too many blind alleys.
I'm also aware of open source projects like XSD2Code, but I'd rather make the attempt myself first, for learning reasons, and because the resulting tool is likely to be highly specific to my particular scenario.
Best Answer
Leaving aside trivalities like UI, you'll want to have at least three major parts:
You'll want #1 to have a fair bit of available recursion, since XSD elements can be of types that contain elements and types and elements.
I'd load the whole XSD as a single System.XML.XMLDocument class, but I'm from the web and learned XML via the DOM. It's also the older model, and starting with things like
selectSingleNode
is probably better to start out with than performance-optimizing interfaces likeXmlReader
, or non-microsoft XML parsers.Why wouldn't it be a namespace of structs or classes?
There are no better ways to output strings from a program than concatenating strings. There is just syntactic sugar to make the code easier to write. But you want to use a System.Text.StringBuilder here, instead of mucking about with code like follows: