Working With Resources

AutoCAD Map 3D Geospatial Platform API

 
Working With Resources
 
 
 

Resources in the repository can be managed by using the MgByteSource, MgByteReader, and MgByteSink objects.

To convert a string representation of the XML data into data for a resource, first create an MgByteSource. New MgByteSource objects can be initialized with a string or the contents of a file. Get an MgByteReader from the byte source and pass that to MgResourceService.SetResource().

For example, if an external file contains the string representation of the XML for a layer definition, the following would read the contents of the file and store it in the repository.

MgResourceIdentifier rID_layer = new MgResourceIdentifier(
    @"Library://Data/Zoning.LayerDefinition");
MgByteSource layer_byteSource = new MgByteSource(xmlFilePath);
layer_byteSource.SetMimeType("text/xml");
resourceService.SetResource(rID_layer, 
  layer_byteSource.GetReader(), null);

There are many ways of modifying the XML in a resource. The best method to use in a given situation depends on the development environment, the developer’s familiarity with a given technique, and the desired result. Some possibilities are described in the following sections. Other techniques for working with XML data are equally valid.

xsd.exe

For .NET development, used with AutoCAD Map 3D and the ASP.NET API of MapGuide, it is possible to use the tool xsd.exe, which is available with the SDK that comes with Microsoft Visual Studio. xsd.exe reads an XML schema and generates .NET classes for working with XML documents based on the schema. xsd.exe can generate both VB.NET and C# classes.

To use xsd, a typical command is:

xsd.exe LayerDefinition-1.0.0.xsd /c /l:cs /n:OSGeo.MapGuide.Schema.LayerDefinition 

For an example of how to use these classes, see the Developer Samples Guide.

For more information on xsd, see http://msdn.microsoft.com.

layerdefinitionfactory.php

MapGuide includes layerdefinitionfactory.php, which contains PHP methods for working with the LayerDefinition schema. These methods are designed to handle most basic tasks with layer definitions, but do not provide complete support for all possibilities. They create the XML by substituting strings into external templates. It is relatively easy to adapt the techniques from layerdefinitionfactory.php for use with a different schema or to translate them into one of the other development languages.

Autodesk MapGuide Studio

One of the simplest ways to create XML templates is to use Autodesk MapGuide Studio with Autodesk MapGuide Enterprise or MapGuide Open Source. Using Studio, create the resources with the desired values, then save them as XML. The resulting file can be edited using a text editor, XML editor, or with techniques like those in layerdefinitionfactory.php.

Document Object Model

Finally, all of the development languages support versions of the Document Object Model (DOM) API, which is a general-purpose API for working with and validating XML files. Using the DOM API requires converting data into a string, then converting that string into an XML document. For example, the following takes resource data from the repository and loads it into an XML document:

// using System.Xml;
 
MgByteReader byteReader = 
  resourceService.GetResourceContent(resourceId);
XmlDocument doc = new XmlDocument();
doc.LoadXml(byteReader.ToString());

Modify the XML document using standard DOM methods, then convert it back to a string and re-save the resource.

String xmlString = doc.DocumentElement.OuterXml;
resourceService.SetResource(resourceId, 
  new MgByteReader(xmlString, "text/xml"), null);