Sometimes you need to use not only the schema but also the class of an object you need to use.
If the schema has imported schemas in its definition, there is a little trick to know if you wan to generate its class.
Let’s say you have a schema Customer that has Address and BillingInfo nodes imported from other schemas, and you attempt to create the Customer class using xsd.exe, you’ll probably do this:
c:\Program Files\Microsoft Visual Studio 8\VC>xsd /c /l:cs /o:C:\GeneratedClasses\ “C:\Projects\Schemas\customer.xsd”
But because you have imported schemas, it won’t work and you’ll get this error:
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Schema validation warning: The ‘http://ImportedSchemas.address:Address‘ element is not declared. Line 21, position 10.
Schema validation warning: The ‘http://ImportedSchemas.BillingInfo:BillingInfo‘ element is not declared. Line 22, position 10.
Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.
Error: Error generating classes for schema ‘C:\Projects\Schemas\customer’.
- The element ‘http://ImportedSchemas.address:Address‘ is missing.
If you would like more help, please type “xsd /?”.
The thing to do is to specify all 3 schemas in the command.
This will generate one single file with all 3 classes.
So if you do
c:\Program Files\Microsoft Visual Studio 8\VC>xsd /c /l:cs /o:C:\GeneratedClasses\ “C:\Projects\Schemas\Address.xsd” “C:\Projects\Schemas\BillingInfo.xsd” “C:\Projects\Schemas\customer.xsd”
Then you’ll get
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file ‘C:\GeneratedClasses\customer.cs’.
And you can use your class.
