Extract Element of Node from XML in Dynamics 365 Finance and Operation using X++ (Parse XML)
Syed Amir Ali
Microsoft Certified Dynamics 365 Finance & Operations Specialist | MCT | Techno-Functional Supply Chain Consultant
When working with XML data in Dynamics 365 Finance and Operations, developers often need to extract specific elements from an XML document. This guide explains how to parse an XML file and retrieve the desired node or element using X++.
Overview
In X++, the XmlDocument class provides robust functionality for parsing and manipulating XML data. You can use it to navigate through XML nodes and extract specific element values efficiently.
Example Scenario
Consider the following XML structure:
<?xml version="1.0"?>
<ocs>
<meta>
<status>ok</status>
<statuscode>200</statuscode>
<message>OK</message>
</meta>
<data>
<id>35</id>
<share_type>3</share_type>
<uid_owner>XXXXXXXX</uid_owner>
<displayname_owner>XXXXXXXXXXX</displayname_owner>
<permissions>29</permissions>
<can_edit>1</can_edit>
<can_delete>1</can_delete>
<stime>17369408752</stime>
<parent/>
<expiration/>
<token>XXXXXXXXXXX</token>
<uid_file_owner>XXXXXXXXX</uid_file_owner>
<note></note>
<label></label>
<displayname_file_owner>XXXXXXXXXX</displayname_file_owner>
<path>/demo.pdf</path>
<item_type>file</item_type>
<item_permissions>30</item_permissions>
<is-mount-root></is-mount-root>
<mount-type></mount-type>
<mimetype>application/pdf</mimetype>
<has_preview></has_preview>
<storage_id>test::dynamics365</storage_id>
<storage>4</storage>
<item_source>1511</item_source>
<file_source>1511</file_source>
<file_parent>1186</file_parent>
<file_target>/demo.pdf</file_target>
<item_size>505682</item_size>
<item_mtime>17368565765</item_mtime>
<share_with/>
<share_with_displayname>XXXXXXXXXX</share_with_displayname>
<password/>
<send_password_by_talk>XXXXXXX</send_password_by_talk>
<url>https://abc.com/s/nGZL8pL757QKJF</url>
<mail_send>1</mail_send>
<hide_download>0</hide_download>
<attributes/>
</data>
</ocs>
Desired Output
When the code runs successfully, the extracted URL value is displayed in the Infolog:
领英推荐
<url>https://abc.com/s/nGZL8pL757QKJF</url>
X++ Code Snippet
You can use the below x++ method to get the desired output.
public void parseXML(str xmlData)
{
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
System.IO.StringReader stringReader;
System.Text.StringBuilder jsonBuilder;
System.Text.Json.JsonSerializerOptions jsonOptions;
System.IO.MemoryStream memoryStream;
System.Text.Json.JsonWriterOptions jsonWriterOptions;
System.Text.Json.Utf8JsonWriter jsonWriter;
XmlElement rootElement, dataElement, urlElement;
str urlValue;
// Declare variables
XmlDocument xmlDoc;
XmlNodeList productList;
XmlElement productElement;
XmlNode xmlProjectNode;
int i;
try
{
// Load the XML
xmlDoc = XmlDocument::newXml(xmlData);
// Get the root element (<ocs>)
rootElement = xmlDoc.documentElement();
// Navigate to the <data> element
dataElement = rootElement.selectSingleNode('data');
if (dataElement)
{
// Retrieve the <url> element
urlElement = dataElement.selectSingleNode('url');
if (urlElement)
{
// Get the value of the <url> element
urlValue = urlElement.text();
info(strFmt('URL: %1', urlValue));
}
else
{
info('URL element not found.');
}
}
else
{
info('Data element not found.');
}
}
catch (Exception::Error)
{
error("An error occurred while parsing the XML.");
}
}
Happy Learning,
Syed Amir Ali.