Dynamics 365 Business Central – Create an XML File in AL

XML (eXtensible Markup Language) is a standard for encoding documents and is used to structure data for storage and transport. XML files are just plain text files that use custom tags to describe the structure and features of the document. Many describe the file’s structure as self-descriptive and intended to be both human and machine-readable. XML files are also extensible; they don’t have a predefined Markup and allow users to create their own tags and structure.

In today’s digital age, the exchange of data between Microsoft Dynamics 365 Business Central and other systems is often necessary. Because of this, I am frequently asked how to create an XML file in AL. Here is an example of creating XML within AL.

    local procedure CreateXML(): Text
    var
        Declaration: XmlDeclaration;
        XmlDoc: XmlDocument;
        Elements: XmlElement;
        Element: XmlElement;
        ElementItem: XmlElement;
        Comment: XmlComment;
        e,
        i : Integer;
        XmlData: Text;
        XmlWriteOptions: XmlWriteOptions;

    begin
        // Create the XML Document
        XmlDoc := XmlDocument.Create();
        // Create the Declaration
        Declaration := XmlDeclaration.Create('1.0', 'utf-8', 'yes');

        // Add the declaration to the XML File
        XmlDoc.SetDeclaration(Declaration);

        // Create Root Element
        Elements := XmlElement.Create('Elements');

        // Create a comment
        Comment := XmlComment.Create('This is a comment');
        // Add comment to eleement
        Elements.Add(Comment);

        for e := 1 to 2 do begin
            Clear(Element);
            Element := XmlElement.Create('Element');

            // Set element attributes
            Element.SetAttribute('id', Format(e, 0, 9));
            Element.SetAttribute('seq', Format(Time, 0, 9));

            for i := 1 to 3 do begin
                // Create and add data elements to the element
                Clear(ElementItem);
                ElementItem := XmlElement.Create('Item' + Format(i, 0, 9));
                ElementItem.Add(XmlText.Create(Format(e + i, 0, 9)));
                Element.Add(ElementItem);
            end;
            Elements.Add(Element);
        end;

        // Add Elements to document
        XmlDoc.Add(Elements);

        // Set the option to preserve whitespace - true makes it "more human readable"
        XmlWriteOptions.PreserveWhitespace(true);
        XmlDoc.WriteTo(XmlWriteOptions, XmlData);
    end;

The resulting XML is

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Elements>
    <!--This is a comment-->
    <Element id="1" seq="08:32:08.51">
        <Item1>2</Item1>
        <Item2>3</Item2>
        <Item3>4</Item3>
    </Element>
    <Element id="2" seq="08:32:08.51">
        <Item1>3</Item1>
        <Item2>4</Item2>
        <Item3>5</Item3>
    </Element>
</Elements>

The above example demonstrates a basic model to provide enough information to create a simple file; consider it “Getting Started.” Other things may need to be considered, namespaces, for instance, in more complex exchanges. There are many ways to code a solution, and opinion or situation determines the best. Within Microsoft Dynamics 365 Business Central, XML files may also be managed with Xmlports and codeunit 6224 “XML DOM Management.”

Note: The code and information discussed in this article is for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2022 Wave 2 online.

Permanent link to this article: https://www.dvlprlife.com/2022/12/dynamics-365-business-central-create-an-xml-file-in-al/

3 comments

4 pings

    • Vidit on November 20, 2023 at 1:07 pm
    • Reply

    Hi GoodAfternoon,

    I am trying to create an xml with the root node below

    but getting a run time error.

    Could you please provide some help.

    1. The example is not listed and was most likely adjusted by the editor due to structure. Do you have a screenshot?

    • ViTo on February 7, 2024 at 4:19 pm
    • Reply

    You saved me big time! Thank you x 10000 for your work.

  1. […] Source : DvlprLife.com Read more… […]

  2. […] between Microsoft Dynamics 365 Business Central and other systems is often necessary. In a previous post, I covered a basic example of creating an XML file with AL. Along with XML files, I am often asked […]

  3. […] between Microsoft Dynamics 365 Business Central and other systems is often necessary. In a previous post, I covered a basic example of creating an XML file with AL. Along with XML files, I am often asked […]

  4. […] Dynamics 365 Business Central – Create an XML File in AL […]

Leave a Reply

Your email address will not be published.