How to Read and Write Text File Using CommaIO in Dynamics Ax


Description:-

AX provides several ways of writing files. Depending upon scenario, you can use one. Apart from AX native filing, you can also use .NET interop to call .NET classes for reading/writing files. We will be showing few classes which are provided by AX for writing files.

CommaIO

The CommaIo class provides functionality for reading and writing comma-separated files.Writes the content of a container to a file.The entries in the container are treated as fields. The container itself is treated as a full record.
Fields are separated by the delimiter that is specified by using the outFieldDelimiter method. Records are separated by the delimiter that is specified by using the outRecordDelimiter method.

Write Text Using CommaIO

static void ExportinTextCommnIO(Args _args)
{
  CommaIo file;
  container line;
  str filepath;
  container confilter =['.txt','*.txt'];
  A_PurchaseOrder objA_PurchaseOrder;
  FileIoPermission permission;
  filepath = WinAPI::getSaveFileName(
    0,
    confilter,
    "",
    "Save As",
    "",
    "Untitled")
  #File
  ;
  permission = new FileIoPermission(filepath, #io_write);
  permission.assert();
  file = new CommaIo(filepath, #io_write);
  file.outRecordDelimiter(#delimiterCRLF);
  file.outFieldDelimiter(' | ');// for semicolon seperator
  if (!file || file.status() != IO_Status::Ok)
  {
    throw error("File cannot be opened.");
  }
  while select objA_PurchaseOrder
  {
    line = connull();
    //Set the data into the container
    line = conins(line, 1, objA_PurchaseOrder.Purchase_ID);
    line = conins(line, 2, objA_PurchaseOrder.Vender_Code);
    line = conins(line, 3, enum2str(objA_PurchaseOrder.Status));
    line = conins(line, 4, date2str(objA_PurchaseOrder.Purchase_Date,
                        123,DateDay::Digits2,DateSeparator::Hyphen,
                        DateMonth::Digits2,
                        DateSeparator::Hyphen,DateYear::Digits4));
    line = conins(line, 5, objA_PurchaseOrder.Purchase_Amount);
    file.writeExp(line);
  }
  CodeAccessPermission::revertAssert();
}

Read Text Using CommaIO

static void ReadTextCommaIO(Args _args)
{
  Dialog dialog;
  DialogField dialogFileName;
  Filename filename;
  strtext,filepath;
  FileIOPermission   permission;
  containerconfilter =['.txt','*.txt'];
  CommaIO  commaIO;
  InteropPermission interopPermission;
  containerreadCon;
  str column1,column2,column3,column4,column5,line;
  #File

  filepath = WinAPI::getOpenFileName(
    0,
    confilter,
    "",
    "Save As",
    "",
    "Untitled")
  ;
  interopPermission = newInteropPermission(InteropKind::ClrInterop);
  permission = newfileIOpermission(filepath,#io_read);
  permission.assert();

  commaIO = new CommaIo(filepath, #io_read);   
  commaIO.inRecordDelimiter(#delimiterCRLF);
  commaIO.outRecordDelimiter(#delimiterCRLF);
  commaIO.inFieldDelimiter("|");           
  if (!commaIO || commaIO.status() != IO_Status::Ok)
  {
    throw error("File cannot be opened.");
  }
  readCon = commaIO.read();
  while (readCon)
  {
    line = con2str(readCon);
    info(line);

    //column1 = Conpeek(readCon,1);
    //column2 = Conpeek(readCon,2);
    //column3 = Conpeek(readCon,3);
    //column4 = Conpeek(readCon,4);
    //column5 = Conpeek(readCon,5);
    //info(strFmt("%1,%2,%3,%4,%5",column1,column2,column3,column4,column5));
    readCon = commaIO.read();
  }   
  //try
  //{
    //if (commaIO)
    //{
      //readCon = commaIO.read();
      //while (commaIO.status() == IO_Status::OK)
      //{
        //readCon = commaIO.read();
        //column1 = conPeek(readCon, 1);
      //}
    //}
  //}
  //catch
  //{
    //warning(strFmt("Importing file failed!"));
  //}   
}

Related Posts

Previous
Next Post »

Thanks for comments.....