Microsoft Dynamics 365 Blog

This post if for when you need to export data in the form of XML from Microsoft Dynamics NAV. If you are not familiar with how to program with MSXML DOM, then it can take a lot of time and work to create even a simple XML document. The purpose of this post is to show a simple example, and to get you started programming with MSXML DOM from NAV.

 I have split this into two posts: “How to write” (this post), and “How to read” an XML Document (click here), since the methods and functions used are quite different.

Why not just use XML-Ports? You can of course, but then you limit yourself to the functionality and limitations that exist in XML-Ports. An example of a limitation in XML-Ports is that they do not support namespaces. If you think that one day you may need functionality that is not supported by XML-Ports, and for which you have to use MSXML DOM anyway, then consider to just use MXSML DOM from the start. It is more complex to begin with, but once you have a set of functions to add / search etc elements in an XML document, then it does not have to be much more complex than using XML-Ports. Note that it is not possible to access an XML docuemnt via MSXML DOM from an XML-Port while the XML-Port is accessing it. The two methods are mutually exclusive.

An XML document is organized as a heararchy of elements, and everything (elements and data) gets added piece by piece to existing elements.

Let’s get started with the simplest possible XML DOcument:

Create a new codeunit with 3 variables:
Name     DataType Subtype Length
XMLDoc     Automation ‘Microsoft XML, v4.0’.DOMDocument 
DOMNode     Automation ‘Microsoft XML, v4.0’.IXMLDOMNode 
DomTextNode     Automation ‘Microsoft XML, v4.0’.IXMLDOMText 

In this example I use MSXML version 4, but you can use version 5 or 6 if you prefer. Version 4 is mostly used because it most likely contains all the functionality you will need, and it will be backward compatible to more machines – even old ones.


Initialize the document:


Unless you have specific reasons not to, set asynch = FALSE. Especially when you are reading an XML document. It means that it will load the whole document into memory before it starts reading through it. If you don’t set this property, you may begin to process the document before it is completely loaded into memory.

Now create a new node, and then add it to the document:

DOMNode := XMLDoc.createNode(1,’NodeName’,”);

Then add some data. This is done in the same way as you add an element: Creat the element first, and then add it to the right place in the document:

DOMTextNode := XMLDoc.createTextNode(‘NodeValue’);

You can add any additional elements like this: Creat the element of the type that you need (node, text, attribute etc), and then append it to the document. 

To see the document, save it to disk:‘C:\XML\MyXML.xml’);

Note: If you run on Vista, it might not allow you to save files on the root of the c: drive. So create a new folder, for example c:\XML\

Then run this codeunit. You should get a very simple document like this:

If you want to add further nodes to this document then repeat the steps above to create a new node, then append it to an existing node. For example, to add a new node:

Create a new variable:
NewDOMNode : ‘Microsoft XML, v4.0’.IXMLDOMNode

Initialize the new node:
NewDOMNode := XMLDoc.createNode(1,’NewNode’,”);

And add text to the new node:
DOMTextNode := XMLDoc.createTextNode(‘NewNodeValue’);

Now, your xml document will look like this:



Adding namespace and Attributes:

You can add a name space either to the whole document, or to individual nodes. To add a name space to the document, go back to the relevant line above, and specify the 3rd parameter:

DOMNode := XMLDoc.createNode(1,’NodeName’,’MyNameSpace’);

Finally, adding an attribute to a node is similar to adding a text element: Initialize the Attribute, then add it to the relevant node. Use the function SetNamedItem to add attributes. Let’s add an attribute called ID with the value 10000:

Create a new Automation variable:
XMLAttributeNode : ‘Microsoft XML, v5.0’.IXMLDOMAttribute

Initialize the attribute, then add it to the node:
XMLAttributeNode := XMLDoc.createAttribute(‘ID’);
XMLAttributeNode.value := ‘10000’;

The whole codeunit should now look like this:

// Initialize the document
// Create Node and attach it to the document. Use name space MyNameSpacefunction
DOMNode := XMLDoc.createNode(1,’NodeName’,’MyNameSpace’);

// Add data (text) to the node
DOMTextNode := XMLDoc.createTextNode(‘NodeValue’);

// Add an attribute to the node
XMLAttributeNode := XMLDoc.createAttribute(‘ID’);
XMLAttributeNode.value := ‘10000’;

// Initialize a new node:
NewDOMNode := XMLDoc.createNode(1,’NewNode’,”);

// And add text to the new node:
DOMTextNode := XMLDoc.createTextNode(‘NewNodeValue’);

// Finally, save the document for viewing:‘C:\XML\MyXML.xml’);

Further references:

MSXML is further documented in MSXML Software Development Kit (MSXMLSDK). I would recommend that you download this to have the online help available for all of the MSXML DOM functions.


These postings are provided “AS IS” with no warranties and confer no rights.
You assume all risk for your use.


Lars Lohndorf-Larsen (Lohndorf)

Microsoft Dynamics UK
Microsoft Customer Service and Support (CSS) EMEA

We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!