1

I have a problem with code generation using xsd.exe (the one provided with the .NET SDK) and also with Xsd2Code (http://xsd2code.codeplex.com/).

I have the following setup of XSD files:

  • Common.xsd
  • Summary.xsd
  • Detail.xsd

Common defines some types that are used in both Summary and Detail and are therefore both Summary and Detail include the line <xs:include schemaLocation="Common.xsd" />.

Now the problem with all XSD code generation tools I tried is that they only take a XSD file with a top level type (so Summary/Detail) and create classes in ONE namespace.

The problem with this is that if I use these tools I get 2 exact copies of every type in Common.xsd (Namespace.Summary.CommonType and Namespace.Detail.CommonType). When I want to use them in code I always have to convert between them (basically just copy all the values) with is quite a nuisance and leads to quite a lot of overhead and confusion.

Is there any XSD code generation tool that handles includes better (ie. puts included types into an own namespace and only generates them once)?

tshepang
  • 12,111
  • 21
  • 91
  • 136
chrischu
  • 3,047
  • 3
  • 27
  • 44

3 Answers3

1

You should try to list all the XSD files in the command line, it should fix your problem for all but some scenarios that hopefully don't apply to your case.

Please take a look at this post, also on SO, it shows exactly what you need to do.

Community
  • 1
  • 1
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
1

Xsd2Code handles this scenario, you just need to run multiple passes of the command for each xsd file, specifying the namespace..

xsd2code Common.xsd MyNamespace MyNamespace\Common.cs
xsd2code Summary.xsd MyNamespace MyNamespace\Summary.cs /eit+
xsd2code Detail.xsd MyNamespace MyNamespace\Detail.cs /eit+

Each xsd file will be generated into its own source code file (optionally in different namespaces). The /eit+ switch indicates that xsd2code wont generate the included schema types into that file.

MattDavey
  • 8,897
  • 3
  • 31
  • 54
  • 1
    This would be really cool but the switch doesn't work at all (although I added it still all included types are generated). – chrischu Apr 26 '12 at 06:50
  • @chrischu are your xsd files included or imported? There is a subtle difference which could be throwing xsd2code off... – MattDavey Apr 26 '12 at 09:06
0

The following method in Xsd2Code always returns true:

Xsd2Code.Library.Extensions.CodeExtension.ContainsTypeName(XmlSchema schema, CodeTypeDeclaration type)

This prevents the exclusion of included xml schema items.

Alex M
  • 2,756
  • 7
  • 29
  • 35