Create an XML Document in AL
Since my colleagues ask me about this from time to time, I have created a small example for the creation of an XML with the new AL types in Business Central. It contains the creation of the document and some nodes, the appending of attributes and filling with text. This should be enough to master most simple XML requirements.
Alternatively have a look into the XML Management Codeunit.
local procedure CreateXmlDemo(): Text
var
xmlElem2: XmlElement;
xmlElem3: XmlElement;
xmlElem: XmlElement;
XmlDoc: XmlDocument;
XmlDec: XmlDeclaration;
XmlResult: Text;
begin
//Create the doc
xmlDoc := xmlDocument.Create();
//Add the declaration
xmlDec := xmlDeclaration.Create('1.0', 'utf-8', 'yes');
xmlDoc.SetDeclaration(xmlDec);
//Create root node
xmlElem := xmlElement.Create('DemoXMLFile');
//Add some attributes to the root node
xmlElem.SetAttribute('attribute1', 'value1');
xmlElem.SetAttribute('attribute2', 'value2');
//Add DataItems
xmlElem2 := XmlElement.Create('DataItems');
//Add a couple of DataItem
xmlElem3 := XmlElement.Create('DataItem');
//Add text to the dataitem
xmlElem3.Add(XmlText.Create('textvalue'));
//Write elements to the doc
xmlElem2.Add(xmlElem3);
xmlElem.Add(xmlElem2);
XmlDoc.Add(xmlElem);
XmlDoc.WriteTo(XmlResult);
exit(XmlResult);
end;
This snippet results in an xml structure like this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<DemoXMLFile attribute1="value1" attribute2="value2">
<DataItems>
<DataItem>textvalue</DataItem>
</DataItems>
</DemoXMLFile>