Generating XSD from a XML file
Posted by decipherinfosys on October 14, 2008
When working with XML data, we have a need to have the XSD prepared as well. It can be used in many ways when doing integration between the systems – first and foremost, both the parties involved in the integration need the XSD to get an idea of the schema, secondly, one can then enforce the XML schema collection at their end to ensure that the schema that they are getting is proper – this prevents any surprises down the road when we are processing the data.
Generating a XSD manually is an error prone and a very cumbersome process. There are a lot of tools out there that allow you to do so in a graphical user interface. MSFT also has a utility which is called XSD.exe: XML Schema Definition Tool. It can be used for quite a few operations:
a) XML to XSD which is what we are discussing in this post,
b) XDR to XSD: I doubt that people use it for this functionality though,
c) XSD to Classes: To generate runtime classes from XSD schema file,
d) Classes to XSD: The reverse of the above, and
e) XSD to DataSet: This generates the common language runtime DataSet classes from an XSD schema file.
You can read more on XSD.exe from this MSDN post here. If you look at yesterday’s post in which we provided a sample of the XML file, we can use XSD.exe on it to generate a XSD for us.
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>xsd.exe C:\IF_XML.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file ‘C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\IF_XML.xsd
I could have specified an output directory as well to get the output in a different location. If you just do xsd.exe /? you will get all the supported flag options. The generated file looks like this. And there is a GUI version of this utility as well which is called VisualXSD – pretty self explanatory – see the screenshot below: