Problem solve Get help with specific problems with your technologies, process and projects.

Load a DSL domain model instance file programmatically

Here Daniel Cazzulino explains how to load a DSL (domain specific language) domain model instance file programmatically. This requires the .NET type of the domain model and its corresponding designer.

A lot of people now find themselves working with DSL (domain specific language) model instances. Is there a way to load these files programmatically?

Say you have a custom DSL file in a solution. Say you want to process the model on that file using GAT or an AddIn...

or even your custom application code at runtime. You need to deserialize that XML (yes, it's all XML in the end) into an instance of your domain model (root).

You will need two pieces of information: the .NET type of the domain model (that is, the parent of your domain model root concept) and its the corresponding designer. The following code does the trick, allowing you to deserialize a model and return the first instance of the given domain model type (i.e. the model root):

public static TRootConcept GetModelRoot
    (string modelFile, Type modelType, Type designerType)
 // Create a new store to deserialize the instance to.
 Store newStore = new Store();
 Type[] metaTypes = new Type[] {
  designerType };

 // Load these types into the store, so that it knows about them for deserialization.

 foreach (Type subStoreType in metaTypes)
  // TODO: this will not be required in RC+ versions of DSL tools.
  Activator.CreateInstance(subStoreType, newStore);

 // Be version resilient
 int majorVersion;
 int minorVersion;
 GetModelVersion(modelFile, out majorVersion, out minorVersion);

 // Deserialize the file into the store
 using (Stream fileStream = File.OpenRead(modelFile))
  XmlSerialization.DeserializeStore(newStore, fileStream, majorVersion, minorVersion, null, null);

 // Locate the attribute that will give you the Guid of the element to find
 MetaObjectAttribute metaObject = Generics.GetCustomAttribute
 if (metaObject == null)
  throw new ArgumentException(String.Format(

 // Return the first one we find
 foreach (object element in newStore.ElementDirectory.GetElements(metaObject.Id))
  return element;
 return null;


The GetModelVersion helper method simply reads the major/minor values from the XML itself:

private static void GetModelVersion(string modelFile, out int majorVersion, out int minorVersion)
 // Get versions from model:
 //<om:MetaModel MajorVersion="1" MinorVersion="1" xmlns:om="http://Microsoft.VisualStudio.Modeling">
 using (XmlReader reader = XmlReader.Create(modelFile))
  if (reader.LocalName != "MetaModel")
   throw new InvalidOperationException(Properties.Resources.FileDoesNotContainModel);
  majorVersion = Int32.Parse(reader.GetAttribute("MajorVersion"), CultureInfo.CurrentCulture);
  minorVersion = Int32.Parse(reader.GetAttribute("MinorVersion"), CultureInfo.CurrentCulture);

This expert response originally appeared on Daniel Cazzulino's blog.

Do you have comments on this Ask the Expert question and response? Let us know.

Dig Deeper on .NET tutorials, guides and quizzes

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.