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 :
Finance | Project Operations, Human Resources, ...
Answered

Read XML through X++ error

(0) ShareShare
ReportReport
Posted on by 758

Hi Everyone,

Need help on below issue,

I am trying to run below code through X++ job,

static void CHI_ReadXMLTest_RR(Args _args)
{
XmlDocument xmlDoc;
XmlNode commonBOMNode, multilevelBOMNode;
XmlNodeList multilevelBOMList;
int bomInt;
XMLParseError xmlError;
str _xmlMsg = "<CommonBOM><multilevelbom></multilevelbom></CommonBOM>";";


xmlDoc = new XmlDocument();

xmlDoc.loadXML("_xmlMsg");

xmlError = xmlDoc.parseError();
if(xmlError && xmlError.errorCode() != 0)
{
throw error(strFmt("XML Error: %1", xmlError.reason()));
}

multilevelBOMNode = xmlDoc.getNamedElement('multilevelbom');
multilevelBOMList = multilevelBOMNode.childNodes();
for(bomInt = 0; bomInt < multilevelBOMList.length(); bomInt++)
{
info(strFmt("%1", bomInt));
}

}

But when I click on Run, getting below error,

XML Error: Data at the root level is invalid. Line 1, position 1.

This code has worked fine on D365. How can I fix this issue?

Please help.

And if I comment the code related to XML error which looks like below,

{
XmlDocument xmlDoc;
XmlNode commonBOMNode, multilevelBOMNode;
XmlNodeList multilevelBOMList;
int bomInt;
XMLParseError xmlError;
str _xmlMsg = "<CommonBOM><multilevelbom></multilevelbom></CommonBOM>";";


xmlDoc = new XmlDocument();

xmlDoc.loadXML("_xmlMsg");
/*
xmlError = xmlDoc.parseError();
if(xmlError && xmlError.errorCode() != 0)
{
throw error(strFmt("XML Error: %1", xmlError.reason()));
}
*/
multilevelBOMNode = xmlDoc.getNamedElement('multilevelbom');
multilevelBOMList = multilevelBOMNode.childNodes();
for(bomInt = 0; bomInt < multilevelBOMList.length(); bomInt++)
{
info(strFmt("%1", bomInt));
}

}

then after running getting error as,

XmlNode object not initialized.

Stack trace

(C)\Jobs\CHI_ReadXMLTest_RR - line 22

What is solution for this?

I have the same question (0)
  • Verified answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    You are loading string "_xmlMsg" in your xmlDoc, instead of the variable called _xmlMsg.

    So, replace xmlDoc.loadXML("_xmlMsg"); with xmlDoc.loadXML(_xmlMsg);

  • Rhushikesh R Profile Picture
    758 on at

    Thank you so much Nikolas. Error has been resolved. Thank you.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 449 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 422 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans