It happens to have a XSD file, for example to communicate with an exisisting web service.
I have to create a class (in this case, C# language) starting from a XSD file provived from server side. How to do it?

First of all, you need the utility xsd.exe. Here how to retrieve this file.

You can just copy and paste it in your working directory. In this dir, just copy also your xsd file. Open the command prompt and change the location to your working directory and run this command:

xsd.exe MyXSD.xsd /Classes

Sometimes, with particular settings in your xsd file, you can get this error:

Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Schema validation warning: The 'http://www.w3.org/2000/09/xmldsig#:Signature' element is not declared. Line 20, position 11.

Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.

Error: Error generating classes for schema 'G2BDokument'.
  - The element 'http://www.w3.org/2000/09/xmldsig#:Signature' is missing.

If you would like more help, please type "xsd /?".

This is because your xsd need also the file xmldsig-core-schema.xsd. You can simply download it here and copy it in same directory.

Now, the comment to run to avoid the error is

xsd.exe MyXSD.xsd /Classes xmldsig-core-schema.xsd

When it will be done, you can find a new .cs file in your working directory, containing the class generated from your xsd file.

Advertisements