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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to update and return XML file using X++

(0) ShareShare
ReportReport
Posted on by 737

Hi Experts,

I have to update a XML node value and need to return with new XML but using below approach I am unable to return new XML, Can you please help me to sort out this issue

void ReadAndUpdateFreightInXML(XML _xml)
{
XmlParseError xmlError;
XmlNode transactionNode;
XmlNodeList rateList;
XmlNode requestNode;
TMSRate localRate;
XmlDocument responseDoc1 = XmlDocument::newXml(_xml);
//xmlError = responseDoc.parseError();

try
{
//ttsbegin;
if (xmlError && xmlError.errorCode() != 0)
{
xmlError.reason();
}

transactionNode = responseDoc1.documentElement();

rateList = transactionNode.selectNodes(#request); //Request mote
requestNode = rateList.nextNode();

rateList = requestNode.selectNodes(#shipping);//ShippingEntity - 1
requestNode = rateList.nextNode();

rateList = requestNode.selectNodes(#shipping);//ShippingEntity - 2
requestNode = rateList.nextNode();

rateList = requestNode.selectNodes(#shipping);//ShippingEntity - 3
requestNode = rateList.nextNode();

rateList = requestNode.selectNodes(#ratings);//RAtingEntities -
requestNode = rateList.nextNode();

rateList = requestNode.selectNodes(#rating);//RAtingEntities -
requestNode = rateList.nextNode();
//Update price in #TotalRate Node
localRate = any2real(this.returnNamedNode(requestNode, #totalRate));
localRate += 11;
this.writeNode(requestNode,#totalrate, int642str(localRate));

rateList = requestNode.selectNodes(#rate);//RatingEntity -
requestNode = rateList.nextNode();
//Update price in #TotalRate Node
localRate = any2real(this.returnNamedNode(requestNode, #totalRate));
localRate += 11;
this.writeNode(requestNode,#totalrate, int642str(localRate+10));

//Update price in #UnitRate Node
localRate = any2real(this.returnNamedNode(requestNode, #UnitRate));
localRate += 11;
this.writeNode(requestNode,#unitrate, int642str(localRate+10));
//return responseDoc1;
}
catch
{
throw Exception::Error;
}
}

*This post is locked for comments

  • Mea_ Profile Picture
    60,286 on at

    But you method does not return anything. So what is the issue?

  • B K Sharma Profile Picture
    737 on at

    Hi ievgen,

    I am able to update node value using mentioned approach but not able to return updated XML, IS there any way so that I can return final XML after node value update

    Thanks

  • Suggested answer
    Mea_ Profile Picture
    60,286 on at

    You method accepts xml string and returns nothing. If you want to return new xml string then return it from the method and assign it to something or save it somewhere.

  • Blue Wang Profile Picture
    Microsoft Employee on at

    Hi BK,

    You can refer this blog: 

    https://mfmujahidmim.wordpress.com/2016/01/19/exporting-data-to-an-xml-file/

    XmlDocument xmlDoc;
    XMLWriter xmlWriter;
    xmlWriter = XMLWriter::newFile(@”C:\temp\test.xml”);
    xmlDoc.writeTo(xmlWriter);
    
    OR
    
    XmlDocument xmlDoc;
    #define.filename(@’C:\temp\test.xml’);
    xmlDoc.save(#filename);


    I  hope it will help 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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
nmaenpaa Profile Picture

nmaenpaa 2 Moderator

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans