45

I am running Visual Studio 2010. I have a XSD schema and want to use xsd.exe tool to generate appropriate C# file. I have done this successfully from a command line but now I want to do the same from IDE.

Is there some well-known route for this? I have managed to use a pre-build event but I hate to have the tool running on each build. Normally, it should only run when XSD changes.

Adding the generated .cs file to project puts it under .xsd of the same name, so the IDE knows they are related.

I know I can set "Custom Tool" on my .xsd but I am not sure if that achieves the desired effect. I was hoping for a more straightforward way.

And ultimately, VS2010 cannot execute XSD.EXE because it lives in the SDK folder and the path known to VS obviously does not include it.

Any suggestions are welcome!

Matthew
  • 1,630
  • 1
  • 14
  • 19
wpfwannabe
  • 14,587
  • 16
  • 78
  • 129
  • It is 2019, and we are trying to revisit this XSD/XML Story, you can vote for better tooling on this link https://developercommunity.visualstudio.com/content/idea/426740/better-xsd-xml-tooling.html – Tony Jan 14 '19 at 20:46

2 Answers2

40

You can check out Xsd2Code for a VS addin that does this interactively. The author is also working on a VS2010 version, as far as I know.

alt text http://i3.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=Xsd2Code&DownloadId=41336

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Thanks! I was hoping for something that would use XSD.EXE from Windows SDK but this is OK too. And btw, the add-in works with VS 2010. – wpfwannabe Jun 07 '10 at 20:06
  • 1
    This solution requires the installation of the tool into all instances of visual studio. Do you know of anything that does not require installation of a plugin? – yamspog Jun 08 '10 at 18:14
  • @yamspog: no, I don't - all the other solutions I know are just different plugins / extensions for VS, basically. – marc_s Jun 08 '10 at 19:46
  • 2
    [See here](http://stackoverflow.com/a/4731379/102937) for some caveats and workarounds. – Robert Harvey May 02 '12 at 16:41
27

You can also add it as an external tool (click tools - external tools...) to visual studio (2015). Then just select the xsd schema in visual studio and click tools - xsd to generate the class file.

adding xsd as an external tool

Parameters:

  • Command: C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.1 Tools\xsd.exe (replace with your version of FX tools)
  • Arguments: /c "$(ItemDir)$(ItemFileName)$(ItemExt)"
  • Initial directory: $(ItemDir)
  • Use Output window: yes
Sean
  • 2,033
  • 2
  • 23
  • 28
  • 1
    Thank a lot!! If it is okay I will little bit instruct others...because it is not obvious from begging how to generate that class. By the way to generate class from xsd file do not forget to add that file to project, then open that xsd file and only then press in tool->xsd – Jevgenij Kononov Apr 18 '18 at 09:31