Generate C# class from XSD file.

I have an existing C# file, generated a long time ago from XSD definition.

The XSD files were changed, and proxy class should be regenerated.

I've tried to use XML Schema Definition Tool (Xsd.exe) to generate C# class, but it  returned the error: "The datatype is missing".
I've actually have two XSD files -outer and imported

Thanks to the post: XSD.exe is kicking my butt, man...redux , it pointed me to specify all necessary files on the commandline.

Note that name of generated file is combined from the files listed

I've created the batch file to be able to rerun it  

CallXSD.BAT

@call "C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat" x86

xsd.exe outer.xsd imported.xsd /classes /l:cs /n**:**My_Namespace_

rem rename  outer_imported.cs as outer.cs 

pause

By some reason the generated file created in some non-default encoding, which inserts 3 characters

 in the beginning.It is a minor annoyance, but when I delete them, VS shows me a warning, that wrong encoding could prevent keeping history.

The next issue was that the new XSD(.Net Framework 2.0 and 3.5) generates C# classes differently with what 1.0/1.1 XSD.exe did.

In a few places 1.1 version generated custom collection of objects, but new XSD.EXE generates array of objects, so I have to change the calls to generated classes.

Another problem with XSD generated classes is that it disables  step into debugging even on methods that you  extended in partial classes by specifying DebuggerStepThroughAttribute for classes. Workarounds are described in XSD.exe and DebuggerStepThrough post.  Issue reported to MS Feedback.

I should try An XSD to .NET language code class generator that adds real punch to Microsoft's XSD Tool.
or Sample Code Generator 1.4.2.1

Another issue was that authors of XSD file added   xsd:choice  elements and it creates extra ItemsChoiceType array and enum.I described how I addressed it in my post XmlChoiceCollection class to access XSD generated properties for choice XML elements That's pity, that minor changes in XSD file causes different class properties to be generated and essential code  changes to access the new properties.

This article is part of the GWB Archives. Original Author: Michael Freidgeim

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.