Search code examples
dsldsl-tools

DSL Add Root Element to Serialization


I am looking for help to achieve the following

The Diagram represents a car, users can add engine and colour

when I view the XML it looks like this:

<Car>
  <Engine>BigEngine</Engine>
  <Colour>Pink</Colour>
</Car>

What I would like to do is to wrap the car inside 'vehicle', i.e

<Vehicle>
  <Car>
    <Engine>BigEngine</Engine>
    <Colour>Pink</Colour>
  </Car>
</Vehicle>

I am not sure of the best way to achieve this. I want the model explorer and the generated XML to be wrapped in 'vehicle' but for all other intents and purposes the user is working with a car only

Info: Visual Studio 2010, C# and DSL SDK for 2010


Solution

  • I have fixed this by the following. I am double deriving the Car class and in the Car serializer I am doing this:

    Writing the extra elements:

      public partial class CarSerializer : CarSerializerBase
    {
        public override void Write(SerializationContext serializationContext, ModelElement element, XmlWriter writer, RootElementSettings rootElementSettings)
        {
            // Adds the Model and LobSystem root elements to match that required by the SharePoint BCS
            writer.WriteStartElement("Garage");
            writer.WriteStartElement("Cars");
            base.Write(serializationContext, element, writer, rootElementSettings);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
    

    To be able to read this back in I am overriding the Car LoadModel method in the SerializationHelper and where it is getting the reader I am reading the elements until I get to Car.

    ....

    XmlReader reader = XmlReader.Create(fileStream, settings);
    reader.MoveToContent();
    while (!reader.EOF && !reader.Name.Equals("Car"))
    {
       reader.Read();
    }
    
    reader = reader.ReadSubtree();
    
    //    using (global::System.Xml.XmlReader reader = global::System.Xml.XmlReader.Create(fileStream, settings))
    using (reader)
    {
    

    ....