…meie igapäevast IT’d anna meile igapäev…


Small utils, part three: GetXMLSchema

Filed under: Isiklikud,Programmeerimine — Sander @ 11:19:19

Again, only one util this time – GetXMLSchema.

GetXMLSchema.exe (15 KB, .NET 2.0)
Source (14 KB)

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.

GetXMLSchemaAs 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:

XmlSchemaInference xSchemaInf = new XmlSchemaInference();
XmlSchemaSet xSchemaSet = xSchemaInf.InferSchema(XmlReader.Create(fileName));
foreach (XmlSchema xSchema in xSchemaSet.Schemas())

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.


Lisa kommentaar »

Kommentaare veel pole.

RSS feed for comments on this post. TrackBack URI

Lisa kommentaar

Täida nõutavad väljad või kliki ikoonile, et sisse logida:

WordPress.com Logo

Sa kommenteerid kasutades oma WordPress.com kontot. Logi välja / Muuda )

Twitter picture

Sa kommenteerid kasutades oma Twitter kontot. Logi välja / Muuda )

Facebook photo

Sa kommenteerid kasutades oma Facebook kontot. Logi välja / Muuda )

Google+ photo

Sa kommenteerid kasutades oma Google+ kontot. Logi välja / Muuda )

Connecting to %s

Create a free website or blog at WordPress.com.