16

Are there any Code Generators that would take a well-formed XML document, and write out an XSD schema based on how the XML document is structured?

0

7 Answers 7

15

Best Answer, bar none:

http://xsd2code.codeplex.com

I pulled my hair out for a full 8 hr work day, got nowhere with all the crappy solutions out there (XSD, CodeXS, XSDObjectGen, and others) and am not interested in paying $400 for the stupid commercial tools just to do this.

Then I came across this codeplex project... WOW! Where was this all my life?

And the best part... its actively being worked on, right NOW (2009), not an abaondoned crust pile from 2004 or some lame thing.

Enjoy!

6
  • Looks great. Too bad it doesn't have a command-line version.
    – Igor Brejc
    Feb 9, 2010 at 13:16
  • 1
    Correction: it DOES have a command-line version. Great!
    – Igor Brejc
    Feb 10, 2010 at 19:58
  • Very nice find! This is how code should be generated! May 6, 2010 at 13:05
  • The ongoing dev is nice. It has gone from unuseable (for me) a few bug reports and feature requests and now it's perfect for what I need. Jul 1, 2010 at 21:22
  • 2
    Great tool, but it doesn't look like this does what the question asker is requesting. This creates C# classes from XSD schema, not a schema from a well formed XML document. Not worth a downvote though :)
    – lecrank
    Oct 28, 2011 at 2:51
12

The XSD.exe utility (which is installed with Visual Studio) is capable of creating an XSD file from an XML file.

Here's the link on MSDN

I believe it may also part of the .NET SDK, meaning you would not need a full-blown Visual Studio installation.

4
  • I'm trying to find the file on my harddrive. Where's the darned thing located? Nov 13, 2008 at 21:36
  • if you open a vs command prompt, just type xsd
    – Chris
    Nov 13, 2008 at 21:40
  • Ah. I was looking for the physical .exe file. Silly me. I finally found it in %Program Files%\Microsoft SDKs\Windows\v6.0A\bin Nov 13, 2008 at 21:59
  • Weird, I didnt know xsd could do inference too :) Thanks!
    – leppie
    Nov 14, 2008 at 4:53
7

Visual Studio does it pretty well too... Open XML file.. Choose 'Generate Schema' Command from XML menu

4
  • This command may be running the XSD tool behind the scenes... Unfortuantely, I had to move to a different machine and do not have VS.Net installed yet to verify this. Nov 13, 2008 at 21:54
  • I doubt it's the same tool, the ouput XSD inferred from the same XML file was quite different - and the Visual Studio version looked more correct !
    – joedotnot
    Mar 17, 2010 at 5:39
  • It definitely does run xsd.exe. You may get different results due to the various parameters you can provide to it. Jul 1, 2010 at 21:20
  • I dispute boomhauer's remark here. I have a massive 40 megabyte file which xsd is choking on and Visual Studio manages fine.
    – S Meaden
    May 14, 2015 at 16:26
2

I am a little late to the part, but I find that Businessware Architects CodeXS is very good. I have found that it handles complex schema's much better.

1

I'm note sure it is useful. After all, it cannot infer everything: if an element is present in the XML document, how to know if it is mandatory or not?

Examplotron, to achieve a similar goal, relies, at least partly, on annotations.

1

Try XXSd2Code. It code generates C#, C++, Java and C++/CLI bindings from xsds. XXSd2Code

1

And old question I know but this might help someone: http://www.freeformatter.com/xsd-generator.html. Simple online tool and gets the job done with ease. Perfect for quick XML schema generation.

Not the answer you're looking for? Browse other questions tagged or ask your own question.