C# – What would the general design for an XSD to C# class converter look like

ccode generationdesignxml

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

What would the general design of such an application look like?

Leaving aside trivalities like UI, you'll want to have at least three major parts:

  1. The "core" app, that translates the XML object into a C# string.
  2. The portion that locates and reads the XSD file as XML, and passes each object over to the core app to process.
  3. The portion that writes out the C# strings.

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 assume that I will need to use one of the XML parsers in the .NET framework, but which one?

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 like XmlReader, or non-microsoft XML parsers.

And what will the resulting data structure look like? Will it be an expression tree?

Why wouldn't it be a namespace of structs or classes?

From there, what happens? Do I generate code by concatenating strings, or are there better, more sophisticated ways to do it?

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:

string Line = new String();
Line = "public class " + xmlDoc.localName + "() {\n";
Line += "etc, etc.";
Related Topic