XSD Schema

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 ;)

Advertisements