Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Generating UTF-8 format file without BOM(Byte Order Mark)

Posted on by 575

I am generating a file in UTF-8 File format. When i open my file through notepad, i can see only my data. But a 3 byte sequence BOM (Byte Order Mark) is getting added at the beginning of the file to indicate that it is UTF-8 file format which is invisible. If i open the file through command prompt, i can see that 3 bytes.

reference:      msdn.microsoft.com/.../textio.aspx

                       msdn.microsoft.com/.../textio.new.aspx

My requirement is that , i don't want that 3 byte sequence BOM at the beginning of the file but my file should be in UTF-8 file format.

Is there any way to remove the Byte order mark through X++ code ? Or any other solution for this?

Thanks & Regards

Arun B S

*This post is locked for comments

  • Denis Macchinetti Profile Picture
    Denis Macchinetti 16,444 on at
    RE: Generating UTF-8 format file without BOM(Byte Order Mark)

    Hi

    Instead of use System.Byte[], use CLRObject like :

    CLRObject               bytes, bytesWithOutBOM;

    However, try to use :

    System.IO.File::WriteAllText( fileName, System.IO.File::ReadAllText( filename ) );

    In this way, seems the BOM disappear.

  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Generating UTF-8 format file without BOM(Byte Order Mark)

    Notepad isn't able to work with UTF-8 without BOM. If you want to use Notepad, keep BOM. If you want to continue without BOM, use a more capable text editor (my choice is Notepad++).

  • Arun B S Profile Picture
    Arun B S 575 on at
    RE: Generating UTF-8 format file without BOM(Byte Order Mark)

    Hi Martin,

        I opened the generated file in notepad, File > save as > it is showing encoding type as "ANSI".

    It should show UTF-8.

  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Generating UTF-8 format file without BOM(Byte Order Mark)

    Regarding ANSI - it would be really strange if UTF8Encoding generated something else than UTF-8 (and it seems to work OK for myself). How did you distinguish ANSI from UTF-8? Can you give me an example?

  • Arun B S Profile Picture
    Arun B S 575 on at
    RE: Generating UTF-8 format file without BOM(Byte Order Mark)

    Hi Martin,

          I tried your code, file is getting generated without BOM but the file is in ANSI format.

    Hi Denis,

        I am using AX 4.0 version, where System.Byte[] ...i am getting the syntax error at the first line itself.

  • Suggested answer
    Denis Macchinetti Profile Picture
    Denis Macchinetti 16,444 on at
    RE: Generating UTF-8 format file without BOM(Byte Order Mark)

    Hi

    In addition to what Martin say, try to use the code below :

    System.Byte[]           bytes, bytesWithOutBOM;    

    bytes = System.IO.File::ReadAllBytes( filename );

    bytesLenght = bytes.GetValue(0);

    bytesWithOutBOM = new System.Byte[bytesLenght-3]();

    bytes.CopyTo(bytesWithOutBOM, 3);        

    System.IO.File::WriteAllBytes(filename, bytesWithOutBOM );

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Generating UTF-8 format file without BOM(Byte Order Mark)

    This behaviour of TextIO class is documented ("UTF-8 files begin with a 3-byte byte-order mark sequence…") and doens't seem configurable. Therefore you shouldn't use it if you want generate files without BOM.

    If you use .NET, you can exclude BOM by using properly configured UTF8Encoding. It's done by the parameter of UTF8Encoding's constructor in the following example (written in X++ using .NET Interop):

    System.Text.Encoding encoding = new System.Text.UTF8Encoding(false);
    ;
    System.IO.File::WriteAllText(@'C:\test.txt', "Data 123", encoding);
  • Arun B S Profile Picture
    Arun B S 575 on at
    RE: Generating UTF-8 format file without BOM(Byte Order Mark)

    Hi Martin,

    My job

    static void bank_test(Args _args)

    {

       FileName                          fileName;

       InteropPermission          iopermission;

       Textio                                 txioWrite;

       #define.UTF8Code(65001)

       #define.write('W')

       ;

       fileName = "C:\\Documents and Settings\\abs\\Desktop\\test.CSV";

       iopermission = new InteropPermission(InteropKind::ClrInterop);

       iopermission.assert();

       txioWrite = new Textio(fileName,#write,#UTF8Code);     // #UTF8Code  is to generate UTF-8 file

       txioWrite.write("Data 123");

       txioWrite = null;

       CodeAccessPermission::revertAssert();

    }

    Thanks & Regards,

    Arun B S

  • Arun B S Profile Picture
    Arun B S 575 on at
    RE: Generating UTF-8 format file without BOM(Byte Order Mark)

    here is my code snippet:

          #define.UTF8Code(65001)

    ;

    txioWrite = new Textio(fileName, #write, #UTF8Code);

    txioWrite.write("Some Data");

    txioWrite = null;

          ......

  • Arun B S Profile Picture
    Arun B S 575 on at
    RE: Generating UTF-8 format file without BOM(Byte Order Mark)

    Hi Martin,

       Thanks for your reply. I am currently generating a UTF-8 file with BOM.

    My ultimate requirement is to generate a UTF-8 file without BOM.

    Regards,

    Arun B S

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans