Again, only one util this time – GetXMLSchema.
I needed to load data from XML file to a dataset – and that is very easy using DataSet.ReadXml() method. However, the speed of this was critical, as it was on Windows Mobile PDA – and XML was almost 14 megabytes in size.
But DataSet.ReadXml() actually reads the XML twice – first to get the schema, second to get the data. That is definitely not efficient or fast. But thankfully, DataSet has also method ReadXmlSchema() – and if you use that to read schema, then ReadXml() has to read file only once. So I needed the schema.
As you hopefully know, it is very easy to generate schema from the XML file using Visual Studio – just load XML and select “Create schema” from XML menu. However, not every machine has Visual Studio installed – and installing VS only for getting XML schema is like using nuclear missile to kill flies.
There used to be quite a lot of free standalone tools for this. Microsoft had XSD Inference 1.0 tool, but since GoDotNet website has been closed, it seems to be no longer available.
So I decided to write yet another small helper util to get XML Schema Definition Language (XSD) info from XML, unimaginatively called GetXMLSchema.
There are two ways to get the XML schema in .NET (there might be more, but these are what I’ve used myself). You can load the XML to DataSet with ReadXml() and then call WriteXmlSchema() or create XmlReader() and get schema(s) from it using XmlSchemaInference class (I am almost certain that DataSet.ReadXML() uses XmlSchemaInference internally to get the schema, too).
Because of the simplicity – and because I needed to use the schema with DataSet in any case – I decided to use DataSet for this, but just in case, here is the code for getting the schema with XmlSchemaInference:
Using GetXMLSchema is very simple. Just select the XML file, it will be parsed and save dialog will pop up. If you decide not to save the .xsd file (i.e. press cancel), schema will be shown in the log window.