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().
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.
For .NET development, used with AutoCAD Map 3D and the ASP.NET API of Infrastructure Map Server, 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.
xsd.exe LayerDefinition-1.0.0.xsd /c /l:cs /n:OSGeo.MapGuide.Schema.LayerDefinition
For more information on xsd, see http://msdn.microsoft.com.
Infrastructure Map Server 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.
One of the simplest ways to create XML templates is to use Infrastructure Studio with Autodesk Infrastructure Map Server 2013 or MapGuide Open Source. Using Infrastructure 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.
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());
String xmlString = doc.DocumentElement.OuterXml; resourceService.SetResource(resourceId, new MgByteReader(xmlString, "text/xml"), null);