Storing code in model files using CDATA

February 23, 2021

Model RealTime models are stored in XML files (.emx and .efx). If you have ever opened such a file in a text editor you have probably noticed that code snippets stored in the model can be quite difficult to read due to the encoding of certain characters that is mandated by XML. Here is an example:

Some code snippets are stored as attributes in XML and they can look even worse, since newlines also will be encoded so that the whole code snippet appears on a single line:

Starting with Model RealTime 11.0 2020.33 it's now possible to set a preference so that code snippets will be saved in CDATA sections in the XML file. Thereby the code does not need to be escaped which greatly improves its readability. The preference is called Use CDATA when saving model files and is located on the Modeling preference page. In addition to code snippets, it also affects how documentation texts are stored.

After setting this preference, the code snippets in the examples above will look like this:

Nice and readable code!

Note that older versions of Model RealTime can still read model files that use CDATA. In that sense this feature is backwards compatible. Note, however, that if you edit and save the model in such an old tool version the code snippets will once again be stored without usage of CDATA.

If you decide to start using CDATA in your model files it can be a bit tedious to manually re-save all files in the workspace to use the new format. To simplify you can run a model fixup which will automatically convert all your model files to use CDATA.

For more information about this feature, watch this video.