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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Dauvis Profile Picture
    170 on at

    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#.

  • Verified answer
    Community Member Profile Picture
    on at

    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()));

               }

           }        

       }

    }

  • Community Member Profile Picture
    on at

    Thanks Afnan.

  • Community Member Profile Picture
    on at

    Please mark the answer verified if it solved your problem.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans