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)

Reading XML file data via x++ code

(0) ShareShare
ReportReport
Posted on by

Hi All,

I am trying to read XML data via x++ Code. I have wrote a job which has no errors but still not able to fetch data.

Please Find my code below :

static void KTI_FindStorageId(Args _args)
{

XmlDocument doc = new XmlDocument();
XmlNodeList apiModelList;
XMLNode rootNode;
XmlNodeListIterator iterator;
XmlNodeList xmlResponseList;
XmlNodeList xmlTemplateList;
XmlElement apiModel;
XmlElement firstName;
XmlElement lastName;
XMLParseError xmlError;
str xmlMsg;
int i, j;


// Create the XML Document

doc.load("Purchase.xml");
apiModelList = doc.getElementsByTagName('storageId');
//doc = doc.selectSingleNode('storageId');

iterator= new XmlNodeListIterator(apiModelList);

while (iterator.moreValues())
{
apiModel = iterator.value();
firstName = apiModel.getNamedElement('storageId');

if (firstName)
{
info(strFmt("First Name: %1", firstName.text()));
}

iterator.nextValue();
}
}

/////////////////////////////////////////////////////

In above , I have highlighted a line in red.  Please let me know when I debug why it is not assigning any value to XML Element (firstName) in this case.

what is going wrong please let me know

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    I would suspect the structure of the XML is not in the way you are trying to process it, like it has special attributes or namespaces.

    Have a look at examples where they provide the document itself and read it from code, and compare it against yours.

    Such as:

    donatas.xyz/read-xml-string.html

    community.dynamics.com/.../ax-2012-reading-xml-nodes-under-specific-tag-through-x

    Pasting the XML header and the piece which you cannot read would also reveal what you are doing incorrectly.

    Also did you verify that your XML has been loaded correctly? I do not see any path information provided, maybe that is your problem.

  • Community Member Profile Picture
    on at

    Hi vilmos,

    Thanks for the Quick Reply.  in the code there is line "doc.load("Purchase.xml") which is the path for the file.

    however I am looking at your suggestions

  • Community Member Profile Picture
    on at

    Vilmos,

    Only problem with the line here is "firstName = apiModel.getNamedElement('storageId');"

    while debugging I have noticed that firstName is null.

    when put cursor over apiModel I could see the value which held between the tag <storageId>

    .

    what could be the problem ?

  • Vilmos Kintera Profile Picture
    46,149 on at

    Paste the XML, and please use the Insert Code </> formatting tag in the Use rich formatting editing mode!

    Your code is hard to read and not well-formatted in your original post too due to not using the tag for code pasting...

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

    It seems that apiModel already is the storageId element and your getNamedElement() is trying to get a nested storageId (which probably isn't there, therefore the function returns null).

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