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

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.

Page 2 of 3 | Previous page | Next page