OCLOperators ViewModelAsXml

Purpose

Used to create XML documents based on a ViewModel.

Example:

selfVM.ViewModelAsXml('ReportExportToXML', vCurrent_ReportExportView)

This code will output a valid XML text for saving to a file/downloaded by a client:

vCurrent_ReportExportView.XML := selfVM.ViewModelAsXml('ReportExportToXML', vCurrent_ReportExportView).
replace('<root>', '<?xml version="1.0" encoding="ISO-8859-1"?>').
replace('</root>', '');

New 2021-04-27: You may add the tagged value XmlRootTag on the ViewModel to get your own root tag.

In the example, the root elements are replaced/removed and an XML header is added to make it an XML file (not a DOM representation).

Note: the encoding in the XML header says ISO-8859-1, but the string type in .Net is always Unicode in memory. To save this correctly, convert it upon saving.

You can convert from Unicode to ISO-8859-1 like this (generated XML is stored in self.XML in this example):

self.XML.StringToEncodedBase64(28591).Base64ToBlob

The value 28591 is ISO 8859-1 Latin 1; Western European (ISO) taken from this table: https://docs.microsoft.com/en-us/windows/desktop/intl/code-page-identifiers

Converting it to a Blob is necessary for downloading and will create a download link. Read more here: BlobDownloadLink

ViewModelToXMLexample.png

It will result in this output:

 <?xml version="1.0" encoding="ISO-8859-1"?>
 <SalaryData Created="2020-09-11" CompanyName="Model agency" OrgNo="825002-1295" ProgramName="TrainerTime">
   <TimeCodes>
     <TimeCode Code="11061" TimeCodeName="Timlön inkl semesterersättning" />
     <TimeCode Code="11062" TimeCodeName="Tillägg förstatränare" />
     <TimeCode Code="11063" TimeCodeName="Tillägg hjälptränare" />
   </TimeCodes>
   <SalaryDataEmployee FromDate="2020-08-01" ToDate="2020-08-31">
     <Employee EmploymentNo="001" FirstName="First" Name="Last" FromDate="2020-08-01" ToDate="2020-08-31">
       <NormalWorkingTimes></NormalWorkingTimes>
       <Times>
         

Tagged Value Options

NodeName

If you set the tagged value NodeName of the ViewModel attribute, that name will be used instead of the ViewModel attribute name. This enables you to have, for example, " - " (dash) in the node name. Note that you can't use this to add a namespace to the name. (:xx) - if you need another namespace than the parent, add it as attribute "xmlns" on node.

XmlChildnode

Default for single associations in the ViewModel is to add more elements to the parent with the style <classname>.<attribute name>. If you use XmlChildNode = true, single associations will be rendered as an XML child element instead.

Default for many associations in the ViewModel is to add a child element and then one child element under that for each object in the association. If you use XmlChildNode = false, the first child element will be omitted.

Single-links that don't point to an object will not render anything with XmlChildNode active.

XmlAttribute

Default for attributes in the ViewModel is to be rendered as XML elements. If you use XmlAttribute = true, columns will be rendered as an XML attribute instead.

XmlParentValue

Makes the attribute's value appear as the parent element's value instead of an element. Note! If used on more than one attribute on the same ViewModel class, the result is undefined.

See also:

OCLOperators StringToBase64 for UTF8 encoding.

OCLOperators XsltTransformXml for transforming the created XML to another format.

OCLOperators ViewModelAsJSon to get the same information as JSON

Work in the Debugger With A Live View

2021-04-28 13h15 14.png
This page was edited 169 days ago on 06/17/2024. What links here