The views expressed in this blog are those of the individual and do not necessarily represent those of Microsoft. This information is provided by way of general information only and should not be relied on without obtaining independent expert advice. These postings are provided "AS IS" with no warranties and confer no rights. You assume all risk for your use.

Sample of how to read a simple XML document using MSXML DOM in NAV

This is a follow-up to the post "how to write a simple XML document". The other post conitains a few more background details which I won't repeat here. So even if you only want to read XML Documents, then you may want to have a look there anyway.

But let's get straight to the point. This is how you can read an XML Document from Microsoft Dynamics NAV, using MSXML DOM:

4. Set up a loop to browse through the nodes in the document: XMLNodeList := XMLDoc.childNodes; for i := 0 to XMLNodeList.length - 1 do begin DOMNode := XMLNodeList.item(i); ReadChildNodes(DOMNode); end;

You also have to create ReadChildNodes as a function with one parameter: CurrentXMLNode, Type = Automation 'Microsoft XML, v4.0'.IXMLDOMNode.

The reason for the functionis to enable you to run it recursively. This is useful because you don't know how many child nodes there are. And there can easily be a child node inside of another child node.

The function checks to see what type of node it was called with. A node can be an element, text (data), attribute etc. The node property .nodeType tells you which it is. See the documentation (included in MSXMLSDK) for a complete list of possible nodeTypes.

One of the good things in MSXML DOM is, that it reads the whole document into memory, so you can jump between nodes, and browse them, and select sub-sets based on node names. So you do not have to read a document from top to bottom if you know exactly which parts of the document you are interested in.

First, we can count how many orders the document contains (see how many "Order"-elements are under the root-element):

This will open the XML document, then use the getElementsByTagName-function to get all elements that match the tag name. Note that the tag name you specify is case sensitive (like everything else in XML)! So in this case, it would not find elements called "order".

Once you have the XMLNodeList, then you can go through the nodes in that list, using the first example.

Finally, an example which will read the XML document shown above, and show an Amount-total for each order:

This example uses DomNode.SelectNodes, which will select all matching nodes. If you are looking for an individual node, then you can use DomNode.selectSingleNode which will only return one node (the first that matches).

In an XML document, everything is Text. So you have to convert to numbers where needed. Final note: Make sure to make your code more robust than this! What if Amount contains a character? What if an element is missing? If you don't take these things into consideration, then your code can fail with very unfriendly errors.

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