Skip to main content

Posts

Showing posts from April, 2017

AX 2012 : Remove an XML node through X++

Following code snippet may help you to remove an XML node from an XML message string. private str removeXMLTag( str xml) {     XmlDocument         doc = new XmlDocument();     XmlElement          nodeScript;     XmlNode             parentNode, childNode;     XmlNodeList         xmlScriptList,                         parentNodeList,                         childNodeList;     int                  i,j;     doc.loadXml(xml);     // Get the root element and its child nodes     nodeScript = doc.getNamedElement( "SalesOrder" );     xmlScriptList = nodeScript.childNodes();     for (i= 0 ; i < xmlScriptList.length(); i++)     {         parentNode = xmlScriptList.item(i);         childNodeList = parentNode.childNodes();         for (j= 0 ; j < childNodeList.length(); j++)         {             childNode = childNodeList.item(j);             if (childNode.selectSingleNode( "Notes" ))             {           

AX 2012: Reading files from directory through X++

Following code can be used to read files from the specified path. With this example, I am reading a CSV files from a directory. Class declaration class readFilesFromDirectory extends RunBase {     FilePath            filePath;     Filename            filename;     CommaTextIo         fileIO;     Set                 fileSet;     CommaIo             commaIo;     str                  currentFile;     container            lineCon;     #File     #AviFiles } main method Public static void main(Args _args) {     readFilesFromDirectory  readFiles = readFilesFromDirectory::construct();     readFiles.readFilesFromDirtecory(); } Construct Public static readFilesFromDirectory construct() {     return new readFilesFromDirectory(); } readFileFromDirectory method private void readFilesFromDirtecory() {     SetEnumerator           sE;     int    

AX 2012: Create sales order with delivery address from an XML file

Code snippet to create a sales order from an XML file. private void createSOFromFile() {     AxdSalesOrder   salesOrder;     AifEntityKey    key;         Filename        fileName;     RecId           salesTableRecId;     SalesTable      salesTable;         XmlDocument     xmlDoc;         salesOrder  = new AxdSalesOrder();     fileName    = @"C:\Temp\SalesOrder.XML" ;         xmlDoc = XmlDocument::newFile(fileName);         key = salesOrder.create(xmlDoc.xml(),                 new AifEndPointActionPolicyInfo(),                new AifConstraintList());     salesTableRecId = key.parmRecId();         info( strFmt ( "Sales order created: %1" , SalesTable::findRecId(salesTableRecId).SalesId)); } Sample XML file <?xml version="1.0" encoding="UTF-8"?> <SalesOrder xmlns:n="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">    <SalesTable class=&quo

AX 2012: Reading XML Nodes under specific tag through X++

XML to read X++ Code   For above XML structure, I will read all nodes exist within each InstructorAPIModel node. The getElementsByTagName() method of XmlDocument class can be used which returns XmlNodeList which can then be iterated to get node's values.   XmlDocument doc = new XmlDocument(); XmlNodeList apiModelList; XmlNodeListIterator iterator; XmlElement apiModel; XmlElement firstName; XmlElement lastName; doc.loadXml(_xmlMsg); apiModelList = doc.getElementsByTagName('InstructorApiModel'); iterator= new XmlNodeListIterator(apiModelList); while (iterator.moreValues()) { apiModel = iterator.value(); firstName = apiModel.getNamedElement('FirstName'); if (firstName) { info(strFmt("First Name: %1", firstName.text())); } lastName = apiModel.getNamedElement('LastName'); if (lastName) { info(strF

AX 2012: Reading XML Nodes through X++

XML to read X++ Code XmlDocument     doc; XmlNodeList     xmlScriptList; XmlNodeList     xmlResponseList; XmlNodeList     xmlTemplateList; XmlElement      nodeScript; XmlElement      nodeResponse; XmlElement      nodeTemplate; XMLParseError   xmlError; str  xmlMsg; int i, j;     ;     // Create the XML Document doc = new XmlDocument(); doc.loadXml(xmlMsg); // Verify XML Document Structure xmlError  = doc.parseError(); if (xmlError && xmlError.errorCode() != 0 ) {      throw error( strFmt ( "XML Error: %1" , xmlError.reason())); } // Get the root element and its child nodes nodeScript = doc.getNamedElement( "ns2:return" ); xmlScriptList = nodeScript.childNodes(); for (i= 0 ; i < xmlScriptList.length(); i++) {     nodeResponse = xmlScriptList.item(i);     xmlResponseList = nodeResponse.childNodes();      for (j= 0 ; j <