CodeSmith Community
Your Code. Your Way. Faster!

Generating C# Classes from XSD files

Latest post 03-28-2008 5:05 PM by akidan. 1 replies.
  • 03-24-2008 12:43 PM

    • jbloom
    • Not Ranked
    • Joined on 03-24-2008
    • Posts 1
    • Points 35

    Generating C# Classes from XSD files

    Let me start by saying, I haven't poked around codesmith too much, so I apologize if there is something out there that already explains this.  At the place that I work, we generally design our data structures using XSD files.  The xsd files use <xs:annotation><xs:documentation>... to document elements and attributes.  I would like to be able to convert these XSD files into c# classes similar to the way microsoft's XSD.exe program does, but with the annotations moved into xml summary comments, using generic lists instead of arrays, [insert other xsd.exe shortcomings here (there are many)].  I'm thinking that codesmith would help in this regard, but I'm not sure where to start.  Are there templates for this already that I can use as a starting point?

     Thanks in advance,

    Jeff B.

    • Post Points: 35
  • 03-28-2008 5:05 PM In reply to

    • akidan
    • Top 500 Contributor
    • Joined on 02-14-2008
    • Posts 19
    • Points 433

    Re: Generating C# Classes from XSD files

    Take a look at:

    CodeSmith 4.x Samples -> Xml -> Entity.cst

    This reads in a plain XML file that conforms to EntityMap.xsd and creates a simple class from it.
    It's similar to what you're looking for. It may be a good starting point.

    Theoretically, you could point an XmlProperty's schema to the XSD for XSDs.
    You would also need the DTD for XSD and the DTD for datatypes. When I try this, though, I get a cryptic error:

    There was an error reflecting type '_CodeSmith.pattern'. : There was an error reflecting type '_CodeSmith.element'.

    Worst case, I would consider loading your XSD into an XmlDocument in CodeSmith, and using the XmlDocument's properties to drive your template's generation.

    Hope this gives you some ideas.
    • Post Points: 5
Page 1 of 1 (2 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems