web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Read XML through X++ (Unable to cast object of type)

(0) ShareShare
ReportReport
Posted on by 115

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'

 

 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,959 Most Valuable Professional on at

    As the error says, you try to assign a XmlText object to a XmlElement variable, which isn't a compatible type. Either use a common parent type (such as XmlNode) or - if you need logic specific to XmlElement - check if the object really is a XmlElement (e.g. using the as operator).

  • Ali Zaidi Profile Picture
    4,657 on at

    Why not you write C# code in class library, and then add to aot. It will be more flexible X++.

  • AX QA Profile Picture
    115 on at

    Have you got any example of doing this way?

  • Verified answer
    Faisal Fareed Profile Picture
    10,796 User Group Leader on at

    Hi AX QA,

    Change the error line with this code;

    nodeRes = xmlMessageResponseList.item(j);

    where I declare nodeRes; XmlNode     nodeRes;

  • AX QA Profile Picture
    115 on at

    Ali, Have you got an example with this approch.

  • Martin Dráb Profile Picture
    237,959 Most Valuable Professional on at

    You can use many examples on internet about how to use various approaches in C#. LINQ to XML, for example. Nevertheless you could greatly simplify your code if you used XPath instead of explicitly walking through the tree.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans