After creating an XMLport, you can create a codeunit to run the XMLport or run it from Object Designer. For more information about how to run XMLports from Object Designer, see How to: Run an XMLport from Object Designer. This topic shows you how to create codeunits to run XMLports. Before you create a codeunit, you must first design and save the XMLport. For more information about how to design an XMLport, see How to: Design XMLports.

The basic steps for creating codeunits for export or import are similar. However, the specific code that you write is slightly different.

To create a codeunit, you:

To create a codeunit to run XMLports

  1. In Object Designer, select Codeunit, and then choose the New button to open the C/AL Editor.

    You declare variables to represent the XML document that you want to work with, which is the data file that XMLport will use.

  2. Open the C/AL Globals window and declare a variable that represents the XML document. Set the DataType to File.

  3. Declare a second variable that represents the data stream that will be exported or imported.

  4. If the data stream is for exporting data, set the DataType to OutStream, otherwise set it to InStream.

  5. In the C/AL Editor, in the OnRun trigger, add the following lines of code if the XMLport is for exporting data.

     Copy Code
    varXmlFile.CREATE(“FilePath\myXmlfile.xml”);
    varXmlFile.CREATEOUTSTREAM(varOutputStream);
    XMLPORT.EXPORT(XMLPORT::XMLportName, varOutputStream);
    varXmlFile.CLOSE;
    

    The CREATE Function (File) creates the XML file that will contain the exported data. The CREATEOUTSTREAM Function (File) creates the output stream for the XML file. The EXPORT Function (XMLport) uses the output stream and the name of the specified XMLport to export the data to the XML file.

    If the XMLport is designed for importing data, add the following lines of code.

     Copy Code
    varXmlFile.OPEN(“FilePath\myXmlfile.xml”);
    varXmlFile.CREATEINSTREAM(varInputStream);
    XMLPORT.IMPORT(XMLPORT::XMLportName, varInputStream);
    varXmlFile.CLOSE;
    

    The OPEN Function (File) opens the XML file that contains the data that you want to import. The CREATEINSTREAM Function (File) creates the input stream for the XML file. The IMPORT Function (XMLport) uses the input stream and the name of the specified XMLport to import the data from the XML file to the Microsoft Dynamics NAV database.

    The following table shows the variables that are used in the code.

    Variable Description

    varXmlFile

    Represents the XML file to create for export or open for import.

    MyXmlfile

    The name of the XML file that the exported data will be saved as or the name of the XML file to open for export.

    FilePath

    The path or the location of the XML file that you want to work with, for example, C:\Myfolder\MyXmlfile.xml.

    When exporting data, the code will save the XML file to this location. When importing data, the code will look for the XML file to import from this location.

    varOutputStream

    The output stream that the XMLport uses when exporting data.

    varInputStream

    The input stream that the XMLport uses when importing data.

    XMLportName

    The name of the XMLport to use.

  6. Close the C/AL Editor. In the Save As dialog box, in the ID textbox, enter an ID for the codeunit. In the Name text box, enter a name for the codeunit. Verify that the Compile check box is selected and then choose the OK button.

See Also