31

I have a file in XSD format. How can I convert it to a C# class?

I need class reference in other web applications where I need to make post call as per below:

var res = client.Post<Customer>("/customers", c );
peterh
  • 11,875
  • 18
  • 85
  • 108
Arun Rana
  • 8,426
  • 14
  • 67
  • 107
  • Possible duplicate of [How to generate .NET 4.0 classes from xsd?](http://stackoverflow.com/questions/5217665/how-to-generate-net-4-0-classes-from-xsd) – coding Bott Feb 23 '17 at 12:58

2 Answers2

63

Use the XML Schema Definition Tool xsd.exe found in your framework tools to convert your schema into a serializable class or dataset.

xsd file.xsd {/classes | /dataset} [/element:element]
         [/language:language] [/namespace:namespace]
         [/outputdir:directory] [URI:uri]

And in example, whereas the C# class will be generated in the same directory as the xsd tool:

xsd /c YourFile.xsd
George Johnston
  • 31,652
  • 27
  • 127
  • 172
  • 1
    you do need a `/c` or `/classes` in your command, note - and you *don't* need the language, since c# is the default – Marc Gravell Nov 04 '11 at 13:16
  • @George I need C# code to generate this using above link directly , is it possible or i need to do it after downloading that XSD first? – Arun Rana Nov 04 '11 at 13:32
  • 1
    Might be useful if you knew the location https://stackoverflow.com/questions/22975031/where-to-find-xsd-exe-in-visual-studio-2013-on-windows-8 – Demodave Feb 26 '15 at 15:46
  • 3
    In case you have imported another .xsd from a main .xsd file, we can do like this: `xsd file.xsd import1.xsd import2.xsd /c` – Nguyen Tran May 25 '18 at 08:48
1

you can do like this...

  <xsd xmlns='http://microsoft.com/dotnet/tools/xsd/'>
<generateClasses language='CS' namespace='Namespace.subnamespace'>
    <schema>FirstSchema.xsd</schema>
    <schema>AnotherSchema.xsd</schema>
    <schema>LastSchema.xsd</schema>
</generateClasses>
</xsd>
rockyashkumar
  • 1,302
  • 4
  • 13
  • 24
  • I like where this is going, but can you explain what is reading this file, or how it produces the resulting C# files? – QueueHammer Oct 02 '16 at 04:08
  • for details on how to use the advanced parameter file see here https://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx – Andre Pageot Oct 19 '16 at 09:19