You can export the content of a ViewModel as XML from an OCL expression; this operator is for developers who need to save, download, or transform ViewModel data in an XML-based integration format.
Create XML from a ViewModel
Use ViewModelAsXml on selfVM and provide:
- The name of the ViewModel as a string.
- The current ViewModel instance to serialize.
selfVM.ViewModelAsXml('ReportExportToXML', vCurrent_ReportExportView)
The expression returns XML text representing the ViewModel content. Use an assignment expression when you need to retain that text for an export:
vCurrent_ReportExportView.XML := selfVM.ViewModelAsXml('ReportExportToXML', vCurrent_ReportExportView)
The XML structure follows the attributes and associations exposed by the ViewModel. For example, a ViewModel containing salary data, time codes, and employees can produce XML such as:
<SalaryData Created="2020-09-11" CompanyName="Model agency">
<TimeCodes>
<TimeCode Code="11061" TimeCodeName="Timlön inkl semesterersÀttning" />
</TimeCodes>
<SalaryDataEmployee FromDate="2020-08-01" ToDate="2020-08-31">
<Employee EmploymentNo="001" FirstName="First" Name="Last">
<Times></Times>
</Employee>
</SalaryDataEmployee>
</SalaryData>
Control the document root
By default, the generated XML uses <root> as its outer element. You can choose the outer element name by adding the XmlRootTag tagged value to the ViewModel.
For example, set XmlRootTag to SalaryData on the export ViewModel to generate <SalaryData> as the document root instead of <root>.
Add an XML declaration when required
An XML declaration is not part of the generated root element. If the receiving system requires a declaration, add it to the returned text. The following example replaces the default root start tag with an XML declaration and removes the default closing root tag:
vCurrent_ReportExportView.XML :=
selfVM.ViewModelAsXml('ReportExportToXML', vCurrent_ReportExportView)
.replace('<root>', '<?xml version="1.0" encoding="ISO-8859-1"?>')
.replace('</root>', '');
Use this pattern only when you retain the default root wrapper. If you set XmlRootTag, the generated XML already has the intended root element; do not remove its opening or closing tag.
Configure XML nodes with tagged values
Add the following tagged values to the relevant ViewModel attribute or association to control how it is serialized.
| Tagged value | Apply to | Default behavior | Result when set |
|---|---|---|---|
NodeName
|
ViewModel attribute | The attribute name is used as the XML node name. | Uses the tagged value as the node name. For example, NodeName = pay-code produces <pay-code> rather than the ViewModel attribute name.
|
XmlChildnode
|
ViewModel association | A single association is rendered as elements on its parent in the form <classname.attributename>. A many association has a container child element, with one child below it for each associated object.
|
Set to true for a single association to render it as an XML child element. Set to false for a many association to omit its first container child element.
|
XmlAttribute
|
ViewModel attribute | The attribute is rendered as an XML element. | Set to true to render the value as an XML attribute on the parent element.
|
XmlParentValue
|
ViewModel attribute | The attribute is rendered as its own XML element or XML attribute, according to its configuration. | Makes the attribute value the text value of the parent element. |
Rename a node
Use NodeName when the XML contract requires a name that is not a valid or desirable ViewModel attribute name. For example, a ViewModel attribute can use its normal model name while its XML node is named pay-code.
<pay-code>11061</pay-code>
NodeName cannot add a namespace prefix such as xx:pay-code. When a node needs a namespace different from its parent, add xmlns as an attribute on that node.
Render associations as child nodes
Use XmlChildnode = true on a single association when the associated object must be represented by a child element rather than elements added to its parent.
A single association with XmlChildnode active does not render anything when it does not point to an object. Account for this when the receiving XML format requires an empty element for missing data.
For a many association, use XmlChildnode = false to remove the association's first wrapper element. This is useful when the required XML format expects repeated object elements directly beneath the current parent.
Render values as XML attributes or parent text
Set XmlAttribute = true when a scalar ViewModel attribute belongs in the start tag. For example, attributes configured this way can produce:
<Employee EmploymentNo="001" FirstName="First" Name="Last" />
Set XmlParentValue when an attribute value must become the text of its parent element. For example, the parent can be rendered as:
<TimeCode>11061</TimeCode>
Do not use XmlParentValue on more than one attribute of the same ViewModel class. The resulting XML is undefined.
Prepare XML for download
A .NET string is Unicode in memory. Therefore, an XML declaration such as encoding="ISO-8859-1" does not by itself convert the generated string to ISO-8859-1 bytes. Convert the text when you save or download it so that the actual bytes match the declared encoding.
For an ISO-8859-1 (Latin-1, Western European) export stored in self.XML, convert the string to a Blob:
self.XML.StringToEncodedBase64(28591).Base64ToBlob
28591 is the code page for ISO-8859-1. Converting to a Blob is required for download and creates a download link; see BlobDownloadLink. Ensure that the encoding named in the XML declaration matches the encoding used for the conversion.
Related XML and JSON operations
Use ViewModelAsJSon when the target system requires the same ViewModel information as JSON rather than XML. Use XsltTransformXml when you need to transform generated XML into another XML format. For UTF-8 encoding, see StringToBase64.
