Serialization example?

May 23, 2014 at 5:01 PM
Is there any complete example for serializing a specfic DotSpatial class? I want to serialize a LineScheme object, not an entire project.
May 23, 2014 at 5:46 PM
Figured it out myself!
May 27, 2014 at 7:11 AM
Edited May 27, 2014 at 7:12 AM
It would be very helpful for others if you upload an example of what you made. What do you think?

May 27, 2014 at 10:46 AM
I created a class called something like MapSerializationHelper with two static methods for serializing and deserializing a LineScheme with DotSpatials XMLSerializer class. The latter contains methods both for serialization and deserialization (this was a bit confusing for me at first since .NET framework has separate classes for xml-serialization and xml-deserialization):

Code (I have skipped error handling here):
using System;
using DotSpatial.Serialization;
using DotSpatial.Symbology;
using System.Collections.Generic;
using System.Linq;

public class MapSerializationHelper
  public static string SerializeLegend(ILineScheme scheme)
      XmlSerializer s = new XmlSerializer();
      string result = s.Serialize(scheme);
      return result;

  public static ILineScheme DeserializeLegend(string schemeString)
     var s = new XmlDeserializer();
     var scheme = new LineScheme();
     s.Deserialize(scheme, schemeString);
     return scheme;
So the methods returns a string and takes a string representation of a LineScheme-object, respectively.

Usage example (again, I left out error handling)
Note: _project is an instance of our own class for storing a project object, which is part of our application.
var roadLayer = (LineLayer)_map1.Layers[1];

// Create and save a new legend if no one is available in _project            
if (string.IsNullOrWhiteSpace(_project.RoadMapLegendFile))
    // Note: LegendSymbols.CreateRoadLegend(..) is a static method for creating a legend
    roadLayer.Symbology = LegendSymbols.CreateRoadLegend(_roadFs, RoadCategoryColumnName, MainRoadValues, AllRoadValues);
    // Save legend definition as part of the project
    _project.RoadMapLegendFile = MapSerializationHelper.SerializeLegend(roadLayer.Symbology);
// Use existing legend if available
    roadLayer.Symbology = MapSerializationHelper.DeserializeLegend(_project.MapSettings.RoadMapLegendFile); 
May 27, 2014 at 10:51 AM
Thank you very much Peder