May 16, 2008

Creating .NET objects for Mapguide XML schema Definitions (XSD) using LINQ

Filed under: Mapguide — Tags: , — Darrin Maidlow @ 4:18 am

With the release of Mapguide 2007 I was really excited about the new API and the significant power that it would give us developer types.  Though I now appreciate the work involved with creating a full object API for all the supported development platforms, I was a little sad to see that only a handful of objects had been created for working with the various entity types available in Mapguide.

One of my first tasks with Mapguide was to add some layers to the map, before the viewer had loaded up, save that map in the session repository and then build a layout for the map – again in the session repository.  I tried various ways of working with the XML provided by the resource  service, and read through many of the e-mails on the topic from the Osgeo list, but I just couldn’t get it to work.  So I set out on my mission to get an object based method of getting things done.  First, as per the developer documentation I tried out xsd.exe.  XSD allowed me to get a class skeleton created, but there was a lot of work needed to a rounded API running.   I had been recently investigating the new functionality Microsoft was exposing with LINQ – and I came across LINQ to XSD.  For more information on Linq to XSD the is a good place to start. 

So download and install it.  You will need to have installed first.  In addition to that I’m using Visual Studio 2008 Pro.  I have tried these steps with both Mapguide Enterprise 2008 and  2009.  I have to assume the resulting code will work with Mapguide Open Source as well.

Setting up your project

At this point you should be able to fire up Visual Studio and create a new project.  If you are a VB developer, I’m afraid you’re going to have to dive into a bit of C#, as its the only support language right now.  It’s not that bad.

The new project window should look a little like this:

image

Choose LINQ to XSD Library and give the project a name.  Once the new project is loaded, create a new folder called XSD.  Browse to your Mapguide 2009/OS 2.0 server Schema folder (on a default installation its c:\Program Files\Autodesk\MapGuideEnterprise2009\Server\Schema).  Take a these files and drag them into the new XSD folder.

Back in Visual Studio, select all of the XSD files.  Look in the property window and change the "Build Action" to be "LinqToXsdSchema".

Building the project

At this point, right click on the project in Visual Studio, and choose build.  You should have 173 errors.  This is caused by having multiple, similar versions of some of the XSDs.  Let’s exclude the following files:

  • FdoProviderCapabilities-1.0.0.xsd
  • LayerDefinition-1.0.0.xsd
  • LayerDefinition-1.1.0.xsd
  • LoadProcedure-1.0.0.xsd
  • SymbolDefinition-1.0.0.xsd

Rebuild the project and we should be down to five errors.  I have found that by excluding the following files I was able to get the code to generate:

  • ApplicationDefinition-1.0.0.xsd
  • ApplicationDefinitionInfo-1.0.0.xsd
  • SymbolLibrary-1.0.0.xsd

 

For my purposes, I was not too concerned about being able to work with these entity types programmatically (at least at this time).  If anyone is able to work around and would like to share it, please let me know.

Build the project again, and we will see three new errors.  This time we will need to dig into the generated code to correct them.  For each of the three errors we will want to change the public string name.  For example:

   1: public string DataType {

 

We would change to:

   1: public string Datatype {

 

Change the case of the property name in each of the three errors and we are just about there.  At this point you should have around 42904 lines of code in this file.  Good thing I didn’t try to do this manually.   Take a copy of all these wonderful lines o’ code, and we’re going to paste them into a new class. For the sake of this example, I renamed class1.cs to be OSGeo.Mapguide.Objects.cs and pasted the code in there.  I also wrapped all the code in a namespace to match the file name.

If you need to find the generated code, take a look in the obj folder within your project.  You should find a file named LinqToXsdSource.cs in the appropriate build folder.

Next we need to prevent the XSDs from rebuilding each time.  Select all of your XSD files again, and change the "Build Action" to "None".    Building the project at this point will generate fifty some odd errors.  We can eliminate these by doing a search and replace on global:: and replacing it with an empty string.  Build the project again, and you will find one error.  Simply remove the "global." from the line in question and the project will now build.

At this point, you should have a nice .dll file which you can reference from your projects and access many of the Mapguide entity types using objects.  Be sure to grab a copy of Microsoft.XmlSchema.Linq.dll and distribute that with your new DLL.

For your convenience, I’ve posted the Project, source and binaries

This project maybe have some issues.  I have worked with LayerDefinitions, MapDefinitions, and WebLayouts – but not tested many of the classes here yet.  If you have any feedback or enhancements you want to share – please let me know!

In the next week I will post some examples on how to build maps and layouts – before the viewer has loaded.

6 Comments »

  1. Hi,
    thank you for your publication

    Comment by otman — September 17, 2010 @ 11:30 am

  2. where is the examples ?

    Comment by otman — September 17, 2010 @ 11:44 am

  3. Hi otman, you can find the examples here http://www.geospecialling.com/index.php/2008/05/dynamic-authoring-in-mapguide-enterprise-before-the-viewer-has-loaded/

    They’re a little dated – but I’ve had pretty good results with that code using Mapguide Enterprise 2011 – you will need to upgrade the .NET objects though. That post and code can be found here http://www.geospecialling.com/index.php/2010/07/creating-net-objects-for-mapguide-enterprise-2011/

    Comment by Darrin Maidlow — September 17, 2010 @ 11:48 am

  4. Hello

    I am trying to write a web page that reads coordinates from a database and place them on a map, namely trips. I was wondering whether i could achieve this using MapGuide and C#. As I’m a novice, a sample c# project will definitely help me on this issue.

    Please advise.

    thank you

    Reza

    Comment by Reza — April 14, 2011 @ 12:49 am

  5. Hi Reza,

    The comment above about dynamic authoring should get you started with adding layers to the map with code. However, Mapguide itself could be used to display coordinate information without having to write any code at all.

    Comment by Darrin Maidlow — April 14, 2011 @ 4:54 pm

  6. Wow i was looking for this informattion only and i have got great knowledge from our post.

    Comment by Millard Mustin — September 21, 2011 @ 3:01 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress

Switch to our mobile site