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 in order to export a text file with encoding (UTF-8 in the example below).

In order 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.

    Like

    • 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

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s