Microsoft Dynamics 365 Blog


We have had some partner suggestion for adding Unicode capabilities to the existing Microsoft Dynamics NAV File functions. What we recommend is to use .NET Interop to achieve this functionality.

For example, you can use an instance of the System.IO.StreamWriter class to write to an OutStream or the System.IO.StreamReader class to read from an InStream. You control the encoding by using the System.Text.Encoding class where you select between Default, Unicode or UTF8 encoding.

  • Please note that XMLports in Microsoft Dynamics NAV 2013 directly supports importing and exporting flat text files in MS-DOS, UTF-8, UTF-16 encodings by setting the new TextEncoding property.

Writing Unicode text files

Let’s start with a small example on writing some text to a file in Unicode format.

Declare the following variables:


DataType Subtype
outFile File  
outStream OutStream  
streamWriter DotNet System.IO.StreamWriter.’mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089′ 
encoding DotNet System.Text.Encoding.’mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089′ 

Then write this code, using a suitable path for the outFile:



  streamWriter := streamWriter.StreamWriter(outStream, encoding.Unicode);


  streamWriter.WriteLine(‘Hello World’);




  • You can use ‘Hello World’ as above or a text string with some more special characters as you like. I added some Danish characters..: ‘Hello ÆØÅ World’ to more easily see what’s going on.

 Run the code and verify the file is in Unicode.

  • One way to verify a file is in Unicode is to open it with Notepad and select File/Save As…, and then inspect the Encoding field.

Try change the above example to use encoding.Default and verify the file is in ANSI (codepage) format (for example using Notepad as above).

Please note that if you use C/AL to write directly to the outStream, like this:

outStream.WRITETEXT(‘Hello World’);

this is still handled using MS-DOS encoding and is compatible with previous versions of Microsoft Dynamics NAV.

  • You can open a file in MS-DOS encoding by starting Wordpad and in the Open file dialog select “Text Documents – MS-DOS Format (*.txt)”.

In Microsoft Dynamics NAV 2013, all normal text handling are done in Unicode, so the data that are entered on the pages – like customer names one on the Customer Card – can utilize the Unicode character set, the data can be stored in the database and used in C/AL code.

Let’s see how you can extend the above example with data from the Customer table:

Add the customer variable:


DataType Subtype
 customer Record   Customer

And write this code – you can set a filter if needed:



  streamWriter := streamWriter.StreamWriter(outStream, encoding.Unicode);





  UNTIL customer.NEXT = 0;




 If you open the Customers.txt file with a Unicode file viewer it should contain all the Unicode details you have used for your customer names.

Reading Unicode text files

Similar to the above you can read Unicode text files by using the System.IO.StreamReader class to read from an InStream as you can see in the small example below:

Declare the following variables:


DataType Subtype
inFile File  
inStream InStream  
streamReader DotNet System.IO.StreamReader.’mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089′
encoding DotNet System.Text.Encoding.’mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089′
txt Text  

Then write this code, using a suitable path for the inFile:




  streamReader := streamReader.StreamReader(inStream,encoding.Unicode);

  txt := streamReader.ReadToEnd();


Create the Infile.txt as a Unicode file and add some Unicode characters to it. You should see them in the message box when you run the C/AL code.



I hope these simple examples can get you started with Unicode file handling in C/AL. 


Thanks and best regards!

Hans Kierulff

We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!