Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Problem in reading the XML TAG in X++ AX 2012

Posted on by Microsoft Employee

Hi All,

I am facing issue while reading a specific TAG in XML. Below is my XML in AX 2012. I am trying to read the XML which is in AIFQueueManager from in Dynamics AX (Table\AIFDocumentLog).

<?xml version="1.0" encoding="UTF-16"?>

-<TrvPBSMaindataServiceCreateRequest xmlns="schemas.microsoft.com/.../services">


-<TrvPBSMaindata xmlns="schemas.microsoft.com/.../TrvPBSMaindata">


-<TrvPBSMaindata class="entity">

<AmountCurr>15.84</AmountCurr>

<AmountLocal>15.84</AmountLocal>

<BusinessName>Subway</BusinessName>

<CardNumber>6677</CardNumber>

<CardType>PCARD</CardType>

<CatCode>1122</CatCode>

<CatCodesDesc>Subway</CatCodesDesc>

<ExchCode>USD</ExchCode>

<ExchCodeLocal>USD</ExchCodeLocal>

<Name>"Craft, Tamaria L"</Name>

<TransDate>2015-11-05</TransDate>

<UserField1/>

<UserField2>TX</UserField2>

<UserField3>TX</UserField3>

</TrvPBSMaindata>

</TrvPBSMaindata>

</TrvPBSMaindataServiceCreateRequest>

I need to read different XML TAG Values like "AmountCurr", CardNumber", "
Name". etc,

Is there any way that I can directly find the name of a TAG like "AmountCurr" and get the value from above XML. 

I tried the below code but it was giving error in the red lines- XmlNode object not initialized.

str sourceXMLFile, sNumber, sName, sClass;
XmlDocument xmlDocument;
XmlNodeList nodeList;
XmlNode node;
XMLNodeListIterator xmlNodeListIterator;
Counter counter;
AifDocumentLog messageLog;
XmlElement element;
;

select firstOnly messageLog where messageLog.RecId == 5637146860 ;//5637147013;
sourceXMLFile = messageLog.DocumentXml;
xmlDocument = XmlDocument::newXml(sourceXMLFile);
nodeList = xmlDocument.selectNodes('//TrvPBSMainData');

xmlNodeListIterator = new xmlNodeListIterator(nodeList);
node = xmlNodeListIterator.value();
if(node.selectSingleNode('AmountCur'))
sNumber = node.selectSingleNode('AmountCur').text();
while(xmlNodeListIterator.moreValues())
{
counter++;
// node = xmlNodeListIterator.value();

if(node.selectSingleNode('AmountCur'))
sNumber = node.selectSingleNode('AmountCur').text();
if(node.selectSingleNode('CardNumber'))
sName = node.selectSingleNode('CardNumber').text();
if(node.selectSingleNode('Name'))
sClass = node.selectSingleNode('Name').text();

info(strFmt("Record %1: AmountCur - %2, CardNumber - %3, Name - %4",
counter,
sNumber,
sName,
sClass));
xmlNodeListIterator.nextValue();

Can anyone please help ?

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Problem in reading the XML TAG in X++ AX 2012

    Please mark the answer verified if it solved your problem.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Problem in reading the XML TAG in X++ AX 2012

    Thanks Afnan.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Problem in reading the XML TAG in X++ AX 2012

    Try the following code:

    static void XMLJob(Args _args)

    {

       #define.node('TrvPBSMaindataServiceCreateRequest')

       XmlDocument xmlDocument;

       XmlNode     xmlInformationNode;

       XmlNodeList xmlInformationsNodeList;

       XmlNodeList xmlChildNodeList;

       int         i;

       int         j, k;

       str fileName                = 'C:\\CreditCardImport.xml';

       xmlDocument             = xmlDocument::newFile(fileName);

       info(xmlDocument.documentElement().nodeName());

       xmlInformationsNodeList = xmlDocument.documentElement().childNodes();

       setPrefix("@SYS98689");

       for ( i = 0; i < xmlInformationsNodeList.length(); i++)

       {

           xmlChildNodeList = xmlInformationsNodeList.item(i).childNodes();

           for (j = 0; j < xmlChildNodeList.length() ; j++)

           {

               xmlChildNodeList = xmlChildNodeList.item(j).childNodes();

               for (k = 0; k < xmlChildNodeList.length() ; k++)

               {

                   xmlInformationNode = xmlChildNodeList.item(k);

                   info(strFmt('%1 -> %2', xmlInformationNode.nodeName(), xmlInformationNode.innerText()));

               }

           }        

       }

    }

  • Dauvis Profile Picture
    Dauvis 170 on at
    RE: Problem in reading the XML TAG in X++ AX 2012

    I think that you are not selecting nodes using the name spaces properly.  After you instantiate your xmlDocument variable from the file, you need to create a name space manager similar to this.

       xmlNSMgr = new XmlNamespaceManager(xmlDocument.nameTable());

       xmlNSMgr.addNamespace('svc', 'schemas.microsoft.com/.../services&;);

       xmlNSMgr.addNamespace('md', 'schemas.microsoft.com/.../TrvPBSMaindata&;);

    When you select a node, you need to use the abbreviation for the name space that you set up in the manager.  To select your AmountCurr node, you would do something similar to this.  

       amtCurNode = node.selectSingleNode('md:AmountCurr');

    It's been a while since I've had to parse an XML document and I am writing this from memory.  My apologies if I missed a step.  If you need more reference, the classes work very similar to how they work in C#.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans