Generating C# code from XSD

Steve Saxon had posted a few times on the XSD -> C# code generator that he’s building. Looks pretty cool. He also mentions that ideally some of the code should be generated as nested classes. It is true, some constructs such as “xsd:choice” are not easy to translate into an elegant C# equivalent. So, here’s an idea: what if we represent “xwd:choice” as its own class? Possibly a sub-class of some abstract XsdChoice implementation, this class provides solid implementation of properties for each element, but uses the same underpinnings to check the facets and provide the actual “choice” framework. Same for the simple types with restrictions. Then, the public definition of Steve’s ProductType class may go something like this :

public class ProductType : XsdComplexType
{
   public int number {get;set;}
   public howBigChoice howBig {get;}
   public howBig2Choice howBig2 {get;}
   public zipCodeSimpleType {get;}
   public DateTime effDate {get;}
}

public class howBigChoice : XsdChoice
{
   public sizeType size {get;}
   public int height {get;set;}
}

public class howBig2Choice : XsdChoice
{
// same here
}

public class sizeType : XsdType
{
  public bool isValid(int size);
  public int Value {get;set;}
}

public class zipCodeType : XsdType
{
  public bool isValid(string zipCode);
  public string Value {get;set;}
}

What do you think?

TrackBack

Leave a Reply