locked
Multiple Namespaces in XSD Generated Code RRS feed

  • Question

  •  I am using Xsd.exe to generate C# classes from an XML schema. My "primary" schema imports a second schema containing simple type declarations in a different namespace than the primary schema. My application requires that the  type names declared in both schemas remain as originally defined in their respective schemas. (Legacy code integration)

    An issue surfaces when Xsd.exe generates classes for the schema and its imported schema. Both schema contain a similarly named type, only differentiated by their respective  namespaces. Xsd.exe, apparently, generates classes only within the namespace provided via the command line argument. Xsd.exe does not carry forward any "original namespace" information from the sourced schemas, so in order to create unique names in the single generated namespace, one of the type names is "mangled" to resolve the collision of the now duplicated type names, e.g. "foo" becomes "foo1" to distinguish it from the other "foo".

    I'm looking for confirmation of this behavior from a knowledgeable Xsd.exe guru and any ideas on how to work around this problem. I can not change the names used in the schemas to eliminate the duplicated type name occurrence.

    I can offer that code generators from XMLSpy and Liquid Technologies process the schemas without mangling the original type names, each of these tools carry the namespaces used in the schemas into the C# domain when generating the C# classes.

    However, I would like to not introduce these tools into our Visual Studio development environment.

    Any ideas?

    -Chip

    Friday, January 2, 2009 3:49 AM

Answers

  • I am not xsd.exe guru but from what I can see you can sepcify the namespace for generated classes (option /n). You also can define what element from the schema to process (option /e). May be you could try runnind xsd.exe for the first element and put the resulting classes to namespace n1 and then run xsd.exe again for the other element and put the resulting classes to another namespace.
    Another work-around could be just to create in your code classes deriving from the generated classes.

    Hope this helps.
    Pawel
    Thursday, January 15, 2009 1:05 AM
    Moderator

All replies

  • I am not xsd.exe guru but from what I can see you can sepcify the namespace for generated classes (option /n). You also can define what element from the schema to process (option /e). May be you could try runnind xsd.exe for the first element and put the resulting classes to namespace n1 and then run xsd.exe again for the other element and put the resulting classes to another namespace.
    Another work-around could be just to create in your code classes deriving from the generated classes.

    Hope this helps.
    Pawel
    Thursday, January 15, 2009 1:05 AM
    Moderator
  • So what's the solution then? /e or create your own code?

    Friday, March 21, 2014 3:14 PM
  • Whichever works best for you.
    Friday, March 21, 2014 5:01 PM
    Moderator