AX / D365Fo – Add XSI:TYPE attribute in XML files – X++ #ax #msdyn365fo #d365fo

We want to create an XML file with :

  • a namespace in the first element.
  • an XSI prefix for XML elements attributes

Desidered output

<?xml version="1.0" encoding="UTF-8"?>

<FirstElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SecondElement xsi:type="Attribute"/>
</FirstElement>
class temp
{
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        FileIoPermission permission;
        XMLDocument xmlDoc = XMLDocument::newBlank();
        XMLNode rootNode;
        XMLNode NodeEmpl;
        XMLElement xmlElement;
        str ns = 'http://www.w3.org/2001/XMLSchema-instance';

        permission= new FileIoPermission('C:\\Altitudo\\Test.xml','w');
        permission.assert();
        xmlDoc = XMLDocument::newBlank();

        // Create first node with namespace attribute and xsi
        rootNode = xmlDoc.documentElement();
        xmlElement = xmlDoc.createElement('FirstElement');
        xmlElement.setAttribute('xmlns:xsi',ns);
        rootNode = xmlDoc.appendChild(xmlElement);

        //Create a sub-node for the "xsi:type" attribute
        xmlElement = xmlDoc.createElement('SecondElement');
        xmlElement.setAttribute2('type', ns, 'Attribute');
        NodeEmpl = rootNode.appendChild(xmlElement);


        // Save the file
        xmldoc.save('C:\\Altitudo\\test.xml');

    }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s