OCLOperators ViewModelAsXml
No edit summary
(Adding page to Category:TOC because it contains a TOC.)
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
=== Usage ===
=== Purpose  ===
Used to create XML documents based on a viewmodel.
Used to create XML documents based on a ViewModel.


Example;
'''Example:'''
  selfVM.ViewModelAsXml('ReportExportToXML', vCurrent_ReportExportView)
  selfVM.ViewModelAsXml('ReportExportToXML', vCurrent_ReportExportView)
This code will output a valid XML text for saving to a file/downloaded by a client;
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).
  vCurrent_ReportExportView.XML := selfVM.ViewModelAsXml('ReportExportToXML', vCurrent_ReportExportView).
  replace('<root>', '<?xml version="1.0" encoding="ISO-8859-1"?>').
  replace('<root>', '<?xml version="1.0" encoding="ISO-8859-1"?>').
  replace('</root>', <nowiki>''</nowiki>);
  replace('</root>', <nowiki>''</nowiki>);
In the example the root elements are replaced/removed and an XML header is added to make it a XML file (not a DOM representation).
'''New 2021-04-27:''' You may add the tagged value ''XmlRootTag'' on the ViewModel to get your own root tag.


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 you need to convert it upon saving.
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).


You can convert from Unicode to ISO-8859-1 like this (generated XML is stored in self.XML in this example);
'''''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.[[OCLOperators StringToEncodedBase64|StringToEncodedBase64]](28591).[[OCLOperators Base64ToBlob|Base64ToBlob]]
  self.XML.[[OCLOperators StringToEncodedBase64|StringToEncodedBase64]](28591).[[OCLOperators Base64ToBlob|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
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]]
Converting it to a Blob is necessary for downloading and will create a download link. Read more here: [[BlobDownloadLink]]
[[File:ViewModelToXMLexample.png|none|thumb|608x608px]]
[[File:ViewModelToXMLexample.png|none|thumb|608x608px]]


=== Tagged value options ===
It will result in this output:<code>
  <?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>
          <Time DateOfReport="2020-08-31" SumOfHours="8.59" TimeCode="11061" />
          <Time DateOfReport="2020-08-31" SumOfHours="2.34" TimeCode="11063" />
          <Time DateOfReport="2020-08-31" SumOfHours="1.00" TimeCode="11062" />
        </Times>
        <TimeAdjustments></TimeAdjustments>
        <TimeBalances></TimeBalances>
        <RegOutlays></RegOutlays>
      </Employee>
      <Employee EmploymentNo="003" FirstName="Third" Name="Four" FromDate="2020-08-01" ToDate="2020-08-31">
        <NormalWorkingTimes></NormalWorkingTimes>
        <Times>
          <Time DateOfReport="2020-08-31" SumOfHours="7.92" TimeCode="11061" />
        </Times>
        <TimeAdjustments></TimeAdjustments>
        <TimeBalances></TimeBalances>
        <RegOutlays></RegOutlays>
      </Employee>
      <Employee EmploymentNo="004" FirstName="Five" Name="Six" FromDate="2020-08-01" ToDate="2020-08-31">
        <NormalWorkingTimes></NormalWorkingTimes>
        <Times>
          <Time DateOfReport="2020-08-31" SumOfHours="18.50" TimeCode="11061" />
          <Time DateOfReport="2020-08-31" SumOfHours="18.50" TimeCode="11062" />
        </Times>
        <TimeAdjustments></TimeAdjustments>
        <TimeBalances></TimeBalances>
        <RegOutlays></RegOutlays>
      </Employee>
      <Employee EmploymentNo="006" FirstName="Seven" Name="Eight" FromDate="2020-08-01" ToDate="2020-08-31">
        <NormalWorkingTimes></NormalWorkingTimes>
        <Times>
          <Time DateOfReport="2020-08-31" SumOfHours="28.68" TimeCode="11061" />
        </Times>
        <TimeAdjustments></TimeAdjustments>
        <TimeBalances></TimeBalances>
        <RegOutlays></RegOutlays>
      </Employee>
    </SalaryDataEmployee>
  </SalaryData></code>
=== 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 ====
==== 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 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.


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 ====
==== XmlAttribute ====
Default for attributes in the viewmodel is to be rendered as a XML element. If you use XmlAttribute = false, columns will be rendered as an XML attribute instead.
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.


=== See also ===
==== 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 StringToBase64]] for UTF8 encoding.


[[OCLOperators XsltTransformXml]] for transforming the created XML to another format.
[[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 ===
[[File:2021-04-28 13h15 14.png|none|thumb|772x772px]]
[[Category:OCLOperators]]
[[Category:OCL ViewModel Operators]]
{{Edited|July|12|2024}}
[[Category:TOC]]

Latest revision as of 13:48, 26 March 2024

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 47 days ago on 03/26/2024. What links here