As we already discussed the topic in the previous post How to Import Records from Excel Using X++ Code in D365FO. In this article, I would like to show "How to import data from an XML file or Read data from an XML file using x++ code in Microsoft Dynamics 365".
If you are a beginner or learner, you don’t have a dynamic 365 developing or functional environment, you can use Microsoft free Virtual Machine. Learn How to Set Up a Free Virtual Machine for Dynamics 365 Development
What is XML File?
An XML file is an extensible markup language file, and it is used to structure data for storage and transport. In an XML file, there are both tags and text. The tags provide the structure to the data. The text in the file that you wish to store is surrounded by these tags, which adhere to specific syntax guidelines.
At its core, an XML file is a standard text file that utilizes customized tags, to describe the structure of the document and how it should be stored and transported.
Here I am giving one simple example to make you understand how can we read an XML file which is stored in the local directory. This post will help you to read an XML file that is in a simple format as shown below. You can create a Job that reads an XML file.
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<SalesTable RecId="525693083">
<SalesId>SID0001</SalesId>
<SalesName>ABC Trading</SalesName>
<CustAccount>1234567890</CustAccount>
</SalesTable>
<SalesTable RecId="5637163083">
<SalesId>SID0002</SalesId>
<SalesName>XYZ Exporting</SalesName>
<CustAccount>1234567890</CustAccount>
</SalesTable>
</xml>
Here we are using the above simple XML format for reading the XML data. In this example, I hard-coded the path of the XML file which is stored in the local machine by using the x++ code #define.filename(@"D:\Customer.xml") . you can also use file chooser dialogues by adding your own additional codes and choosing the XML file dynamically. The XML file Customer.xml stores the details of customers' using the tags SalesId, SalesName, and CustAccount.
Here I created one Runnable Job(Class),
class XMLReadOperation
{
public static void main(Args _args)
{
XmlDocument doc;
XmlNodeList data;
XmlElement nodeTable;
XmlElement nodeId;
XmlElement nodeName;
XmlElement nodeCustAccount;
#define.filename(@"D:\Customer.xml")
doc = XmlDocument::newFile(#filename);
data = doc.selectNodes('//'+tableStr(SalesTable));
nodeTable = data.nextNode();
while (nodeTable)
{
nodeId = nodeTable.selectSingleNode(fieldStr(SalesTable, SalesId));
nodeName = nodeTable.selectSingleNode(fieldStr(SalesTable, SalesName));
nodeCustAccount = nodeTable.selectSingleNode(fieldStr(SalesTable, CustAccount));
info(strFmt("%1 – %2 – %3",nodeId.text(),nodeName.text(),nodeCustAccount.text()));
nodeTable = data.nextNode();
}
}
}
How it works…
Here we first create a new XmlDocument. We create it from the file and hence we have to use its newFile() method. Then we get all the document nodes of the table as XmlNodeList. We also get its first element by calling the nextNode() method.
Next, we loop through all the list elements and do the following:
1. Get the salesId node as an XmlElement.
2. Get salesname node as an XmlElement.
3. Get custaccount as an XmlElement.
4. Display the text of nodes in the Info log.
5. Get the next list element.
In this way, we retrieve the data from the XML file. A similar approach could be used to read any other XML file.
Hope this post will be very useful for beginners. Please comment and share. In the next posts, I will show how to read XML files from URL in dynamics 365.