Hi All,
I got this XML format to read into AX and following is the code I wrote to read it but it is failing when code runs in CIL.
XML to read
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<ns2:return xmlns:ns2='URL'>
<status>1 to 1 of 1 </status>
<ns2:messageresponses>
<ns2:response>
<from>
<email></email>
<mobile>61417387807</mobile>
<mri>61417387807</mri>
<name>61417387807</name>
<voice></voice>
</from>
<responseCategory>notmatched</responseCategory>
<responseMessage channel='SMS'>
<acknowledged>21/02/17 10:38</acknowledged>
<content>Y</content>
</responseMessage>
</ns2:response>
</ns2:messageresponses>
</ns2:return>
X++ Code
// Define XML Document and its nodes
XmlDocument doc;
XmlNodeList xmlScriptList;
XmlNodeList xmlMessageResponseList;
XmlNodeList xmlResponseList;
XmlNodeList xmlContentList;
XmlElement nodeScript;
XmlElement nodeMessageResponses;
XmlElement nodeResponse;
XmlElement nodecontent;
XMLParseError xmlError;
// Define temporary variables
int i, j, k, l;
// Create the XML Document
doc = new XmlDocument();
doc.loadXml(_xmlMsg);
// Verify XML Document Structure
xmlError = doc.parseError();
if(xmlError && xmlError.errorCode() != 0)
{
throw error(strFmt("XML Error: %1", xmlError.reason()));
}
// Get the root element and its child nodes
nodeScript = doc.getNamedElement("ns2:return");
xmlScriptList = nodeScript.childNodes();
for(i=0; i < xmlScriptList.length(); i++)
{
nodeMessageResponses = xmlScriptList.item(i);
xmlMessageResponseList = nodeMessageResponses.childNodes();
for (j=0; j < xmlMessageResponseList.length(); j++)
{
nodeResponse = xmlMessageResponseList.item(j);
xmlContentList = nodeResponse.childNodes();
for (k=0; k < xmlContentList.length(); k++)
{
nodecontent = xmlContentList.item(k);
if (nodecontent.selectSingleNode("mobile"))
mobile = nodecontent.selectSingleNode("mobile").text();
if (nodecontent.selectSingleNode("acknowledged"))
acknowledgedTime = nodecontent.selectSingleNode("acknowledged").text();
if (nodecontent.selectSingleNode("content"))
content = nodecontent.selectSingleNode("content").text();
}
}
}
The code fails at red highlighted line when executes in CIL with following error message.
System.InvalidCastException: Unable to cast object of type 'Dynamics.Ax.Application.XmlText' to type 'Dynamics.Ax.Application.XmlElement'