Importing the GML 3.2.1 namespace into .NET


There comes a time for every geo-ict professional to have his first encounter with GML. Most of the time, this is not a pretty sight. Until now, I have managed to steer clear from GML when it comes to actually incorporating it into my own software. But today, this day dawned.

So here’s what I’m going to do.  I’m going to generate .NET classes for the GML 3.2.1 schema. When that’s done, I’ll try and actually read a GML 3.2.1 document and do something remotely useful and relevant with it. The second bit will be for a followup post, most probably.

To get started, we’re going to get the combined schemas in one zip file from http://schemas.opengis.net/SCHEMAS_OPENGIS_NET.zip

Then, we unzip those into a directory, say c:\ogc\

We then navigate to C:\ogc\gml\3.2.1 and open a VS2008 command prompt there.

Now we are all set to startto generate .NET classes using xsd.exe

First try is

xsd.exe gml.xsd /classes

This generates a whole bunch of error messages

Screenshot of VS2008 command line box

Some research into the limitations of xsd.exe reveals that the schemaLocation attribute in an import element is ignored by xsd.exe.

from http://msdn2.microsoft.com/en-us/library/ew6ts9yw.aspx:

For the <import> element, the Xsd.exe tool ignores the schemaLocation attribute, expecting imported files instead as additional command-line arguments.

from http://msdn2.microsoft.com/en-us/library/ms950721.aspx

Q: Why doesn’t XSD.exe support the schemaLocation attribute on imports and includes?

A: The W3C XML Schema recommendation describes this attribute as a hint, which can be ignored by processors that can use alternate means to locate schemas. XSD.exe only uses schemas that are specified through the command line to convert schema A.xsd, which imports schema B.xsd.

xsd.exe /c A.xsd B.xsd

So it turns out we need to specify all schemas referenced using import elements need to be supplied as command line arguments to xsd.exe. That means we need to find all schema files referenced to using import elements in gml.xsd and – cascadingly – in all schema files referenced using include elements. This is one hell of a job which took me about an hour of concentrated gazing at XML schemas to complete.

The reward was an xsd.exe command line that actually worked:

C:\ogc\gml\3.2.1>xsd.exe gml.xsd ..\..\xlink/1.0.0\xlinks.xsd ..\..\iso\19139\20
070417\gmd\gmd.xsd ..\..\iso\19139\20070417\gco\gco.xsd ..\..\iso\19139\20070417
\gss\gss.xsd ..\..\iso\19139\20070417\gts\gts.xsd ..\..\iso\19139\20070417\gsr\g
sr.xsd /classes
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.1432]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\ogc\gml\3.2.1\gsr.cs'.

C:\ogc\gml\3.2.1>

The proof of the pudding is in the screenshot:

The result is a gml.cs C#.NET class file that we shoul be able to use in an XmlSerializer. More in a future post.

About these ads

11 thoughts on “Importing the GML 3.2.1 namespace into .NET

  1. I am also working with GML and .Net. Another potential pitfall with .Net (xsd.exe) and GML is the use of the xsd:list element by GML (gml:doubleList, gml:integerList). xsd.exe can’t properly handle these lists when defined inside of elements (See gml:DirectPositionType or gml:pos) You won’t get any error when creating the classes but you will at runtime with something like this

    System.InvalidOperationException: There was an error reflecting Type ‘DirectPostitionType’. —> System.InvalidOperationException: There was an error reflecting field ‘Text’. —> System.InvalidOperationException: Member ‘Text’ cannot be encoded using the XmlText attribute

    I have not come up with a solution yet (short of hand coding the relevant classes or hacking GML yucky)

    Here are some relevant links on MSDN for .Net 3.5, 3.0, and 2.0

    http://msdn.microsoft.com/en-us/library/t84dzyst.aspx

    http://msdn.microsoft.com/en-us/library/t84dzyst(VS.85).aspx

    http://msdn.microsoft.com/en-us/library/t84dzyst(VS.80).aspx

  2. Brian, thanks for that. It was indeed the first thing I ran into when I tried to deserialize some sample GML using the generated class code. Sorry for the delay, your reaction ended up in my spam queue.

  3. So… did you ever get anywhere with this. I am having the same issue. It seems this should be like a “once and for all” kind of thing.

    • Unfortunately, I received a custom implementation of the gml-format (“bag-gml.xsd” etc.etc). Do you have any tools / search patterns so I can fix this version manually? I already replaced all the lists (double to string) and i’m now looking for the sequences with only one child element..

  4. Ever consider using LINQ-To-XSD to generate said GML 3.2.1?
    I’ve done it,and works great!
    Only hurdle on the way was the fact that although each of the official OPENGIS schemas are ‘normative’ by themselves,but not they don’t add up to a ‘normative’ schemaCollection.
    This misshap although well documented by the Consortium in the release docs,echoes into the compilation-process and of course … no API.
    But I’ve found that the isssue lies in their use of ‘non-normative use of the xsd:include mechanism.
    So I then just copied&pasted all 23 xsd’s content into 1 unfied xsd schema element,and ‘”fanny’s-y’r-uncle,’fanny’s-y’r-aunt” … she works fine.
    enjoy!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s