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

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

XML read with X++ (Not working)

(0) ShareShare
ReportReport
Posted on by 115

Hi Folks,

I have following XML and looking for a way to read data for each node element under instructorApiModel

<ArrayOfInstructorApiModel xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.datacontract.org/.../07">
<InstructorApiModel>
<Address>TestAddress</Address>
<AxId></AxId>
<Email>tester@email.com</Email>
<FirstName>TestFirstName</FirstName>
<LastName>TestLastName</LastName>
<MiddleName>TestMiddleName</MiddleName>
<PhoneNumber>1234567890</PhoneNumber>
</InstructorApiModel>
<InstructorApiModel>
<Address>Ukraine</Address>
<AxId></AxId>
<Email>test@gmail.com</Email>
<FirstName>Rob</FirstName>
<LastName>yan</LastName>
<MiddleName i:nil="true" />
<PhoneNumber>1234567891</PhoneNumber>
</InstructorApiModel>
</ArrayOfInstructorApiModel>

I have following code in X++ but its not working - can anyone please suggest me what I am missing?

// Create the XML Document
doc = new XmlDocument();
doc.loadXml(_xmlMsg);

data = doc.selectNodes("//ArrayOfInstructorApiModel");

xmlNodeListIterator = new xmlNodeListIterator(data);
while(xmlNodeListIterator.moreValues())
{
node = xmlNodeListIterator.value();

if(node.selectSingleNode('FirstName'))
templateId = node.selectSingleNode('FirstName').text();
info(strFmt("First Name: %1", templateId));

if(node.selectSingleNode('LastName'))
templateName = node.selectSingleNode('LastName').text();
info(strFmt("First Name: %1", templateName));

xmlNodeListIterator.nextValue();
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    236,316 Most Valuable Professional on at
    RE: XML read with X++ (Not working)

    I see several problems there. First of all, your code doesn't select anything - that's something you should have tested and found before even start writing the while loop. The problem is that you don't work correctly with namespaces. To keep it simple, my code below work around this problem by using a different way to query XML.

    Your code wouldn't work anyway, because you would select ArrayOfInstructorApiModel, while your code inside the loop expects that the list contains InstructorApiModel. And you have a few smaller issues there.

    I suggest you use this:

    XmlDocument         doc = new XmlDocument();
    XmlNodeList         apiModelList;
    XmlNodeListIterator iterator;
    XmlElement          apiModel;
    XmlElement          firstName;
    XmlElement          lastName;
    
    doc.loadXml(_xmlMsg);
    
    apiModelList = doc.getElementsByTagName('InstructorApiModel');
    iterator= new XmlNodeListIterator(apiModelList);
    
    while (iterator.moreValues())
    {
        apiModel = iterator.value();
            
        firstName = apiModel.getNamedElement('FirstName');
        if (firstName)
        {
            info(strFmt("First Name: %1", firstName.text()));
        }
            
        lastName = apiModel.getNamedElement('LastName');
        if (lastName)
        {
            info(strFmt("Last Name: %1", lastName.text()));
        }
    
        iterator.nextValue();
    }
  • AX QA Profile Picture
    115 on at
    RE: XML read with X++ (Not working)

    Worked perfectly - thanks for your explanation Martin.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#2
Guy Terry Profile Picture

Guy Terry 2 Moderator

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans