Text encoding in NAV 2009 R2 Classic Client – Part 2

As I already explained in this blog post you can import text files with encoding in Dynamics NAV 2009 R2 Classic Client and old versions.

Today, I will use the same trick to export a text file with encoding (UTF-8 in the example below).

To illustrate this, let’s export all items in a text file from a Russian database. I’ll use three possible solutions:

1- Via dataport:

Dataport-Export Item.jpg

2- Via codeunit using variable of type File:

Codeunit File-Export Item.jpg

3- Via codeunit using variable of type Automation (Stream):

Codeunit Stream-Export Item.jpg

Now, let’s see the results:

Result.jpg

We quickly notice that export via dataport or via File variable does not do the job as Russian characters are not exported correctly whereas export via Stream does the job perfectly.

As always, I let you download the objects and files I used in this blog post.

This blog is available also on Microsoft Dynamics NAV Community

2 thoughts on “Text encoding in NAV 2009 R2 Classic Client – Part 2”

  1. Great idea, thanks a lot. I extended this to convert some exotic charset files like Shift_JIS to UTF8 so that these may be imported into the current NAV systems with unicode support. Works fine, but since we should not use the COM interface anymore I also looked for the equivalent DotNet methods.
    Do you happen to know which DotNet object has to be selected in C/SIDE? I’ve been searching for quite some time but haven’t been able to locate it.

    1. Hello Kai,

      I’m glad it helps.

      If you want to use .Net (Dynamics NAV 2009 SP1 RTC or higher), I suggest you use System.IO classes. Here is an example how to read a file (of course there is a other ways to achieve this 😉 ):

      Function : GetFileContent(_FilePath : Text) : Text

      FileStream := FileStream.FileStream(_FilePath, FileMode.Open, FileAccess.Read);
      StreamReader := StreamReader.StreamReader(FileStream, DotNetEncoding.UTF8);
      // StreamReader := StreamReader.StreamReader(FileStream, DotNetEncoding.ASCII);
      // StreamReader := StreamReader.StreamReader(FileStream, DotNetEncoding.Unicode);

      EXIT(StreamReader.ReadToEnd);

      Where :
      _FilePath : Text (parameter)
      FileStream : System.IO.FileStream.mscorlib
      FileMode : System.IO.FileMode.mscorlib
      FileAccess : System.IO.FileAccess.mscorlib
      StreamReader : System.IO.StreamReader.mscorlib
      DotNetEncoding : System.Text.Encoding.mscorlib

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top