Generate .Net class from XSD with references to other XSD using xsd.exe

Posted on


I’m currently using a modelling tool for the application design, and below is an example of how we have 2 entities, which in the end will produce 2 xsd files, Transaction.xsd and Person.xsd.

schema

Notice in the Transaction.xsd below, there is a madeBy element with the type of Person which is declared in another Person.xsd (xs:import)

xsd

Now, if we want to generate the Transaction.xsd, it will give us

C:\Schema>xsd.exe Transaction.xsd /c
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.1432]
Copyright (C) Microsoft Corporation. All rights reserved.
Schema validation warning: Type 'https://bembengarifin.wordpress.com/Person:Person' is not declared. Line 8, position 5.
Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.
Error: Error generating classes for schema 'Transaction'.
- The datatype 'https://bembengarifin.wordpress.com/Person:Person' is missing.
If you would like more help, please type "xsd /?".

So how to resolve that is to use


C:\Schema>xsd.exe Person.xsd Transaction.xsd /c
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.1432]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\Schema\Person_Transaction.cs'.

Note: You will need to put the referenced xsd prior to the xsd which is having the references, or else it will throw the same error.

Hope this helps ;)

10 thoughts on “Generate .Net class from XSD with references to other XSD using xsd.exe

    Vikas said:
    July 28, 2009 at 1:57 am

    Dear Bembeng Arifin

    Great Post. Sir, i have an .XSD file which itself contains reference to so many other .XSD files. In your case, you have only 1 reference to another .XSD file so by putting Person.XSD in front of Transaction.XSD might solve the issue but how can i solve my issue with so many references to other .XSD files

    I tried using WSDL tool to generate the code from Contract-First WSDL provided by a company. This WSDL contains references to other .XSD files so i thought WSDL tool will generate the complete code but it threw error. No code was generated

    Can you please help me?

    Thanks

      Bembeng Arifin responded:
      July 28, 2009 at 6:32 am

      Hi Vikas,

      You should be able to generate the XSDs, as long as you put the referenced xsd prior to the xsd which is using it.
      For example.
      – A.xsd has an import reference to B.xsd
      – B.xsd has an import reference to C.xsd

      you can try : xsd.exe C.xsd B.xsd A.xsd
      hope this helps

    Vikas Nanda said:
    July 28, 2009 at 1:39 pm

    Dear Bembeng Arifin

    I have an WSDL file which contains reference to XSD files. I want to run a tool against WSDL file not against XSD file. I thought the tool will pick up WSDL file and its related XSD files and will generate server side code

    Thanks

    Marina said:
    August 4, 2009 at 3:03 am

    Great post! That’s exactly what I need! Thank you very much :)

    Örjan Jämte said:
    October 2, 2009 at 8:52 am

    If you start the code generation from a web service you do the same with wsdl.exe instead of xed.exe.

    wsdl.exe MyService.wsdl MySchema1.xsd MyReferencedSchema2.xsd MyReferencedSchema3.xsd …

    Emil said:
    October 13, 2010 at 5:32 pm

    Hey,

    thats great post but there is a small problem when you run it you get 1 class instead of 2 classes. Ok you may say what is the matter with that? I give you a scenario below.
    there are 2 root xsds and 3 reference xsds
    root: xsd 1 xsd 2 references: xsd A xsd B xsd C
    1 has references A,B,C
    2 has references A,B,C

    when you create classes using the way you illustrated you end up with 2 class both having same properties (property definitions of A,B and C). then if you try to create a WSDL using these 2 classes, Visual Studio will warn you by saying multiple property declarations. Therefore you need to separate them in a common/shared classes again. thats a tired way of doing if you have too many xsds with same references combined.
    ok, now the question is “is there anyway I can accoplish 2+3 classes generated rather than 2 classes for the scenario above?” I know some 3rd party programs doing that but they do sometimes wrong classes. good program suggestion will be appreciated too.

    thanks.

    Surjit Dadhwal said:
    May 29, 2014 at 10:01 am

    Very helpful, exactly what I was looking for

    jesmur said:
    July 3, 2014 at 7:47 pm

    Thank you, this work for me!!

    Murali Manohar Pareek said:
    October 20, 2015 at 6:12 am

    Below command works for creating single file from multiple xsd referring each other.

    C:\Schema>xsd.exe Person.xsd Transaction.xsd /c

    Many thanks :-)

    Vijay Adhikari said:
    April 23, 2016 at 12:41 am

    Very helpful. Thanks.

Leave a reply to jesmur Cancel reply