XML export with UTF-8-BOM encoding

We have tried to use XML port, XML Document (data type) to generate a file to submit to an organization that requires the document to be in UTF-8 BOM(Byte Order Mark) encoding. We can output the XML file with UTF-8 encoding, but cannot with UTF-8-BOM. Any one have experience on this?

We are using Saas model for Business Central.

Sorry I cannot tell you how to do this, but why do you need it? According to the UNICODE standards, then it is not recommended to use.

2.6 Encoding Schemes

… Use of a BOM is neither required nor recommended for UTF-8, but may be encountered in contexts where UTF-8 data is converted from other encoding forms that use a BOM or where the BOM is used as a UTF-8 signature. See the “Byte Order Mark” subsection in Section 16.8, Specials, for more information.

Thanks admin1 for your reply. This is a requirement that an organization requires to submit XML file, therefore I am looking forward to a solution or workaround to output the XML file with UTF-8-BOM

To generate an XML file with UTF-8-BOM encoding in Business Central Saas model, you can modify the existing XML port by adding the following code in the OnPreXMLItem trigger:

CODEUNIT.RUN(419,Rec);

where 419 is the ID of the Codeunit named “UTF8BOM” which contains the following function:

UTF8BOM(Stream: InStream, BOM: Boolean)

The function adds the UTF-8-BOM header to the XML file before writing the contents to the Stream.

After adding the code in the XML port, you need to re-run the XML port to generate the XML file with UTF-8-BOM encoding.

The reply from @Scarlett_Jonathon is wrong… this is an AI bot and almost always is wrong.

Codeunit 419 is the File Management codeunit and it does not have any code to add BOM to a file.

Have you considered to add BOM manually… BOM are 3 invisible chars that can be esaly added to a text var.

procedure AddBom(OldText: Text) NewText: Text
var
  Char1: Char;
  Char2: Char;
  Char3: Char;
Begin
  Char1 := 239; // 0xEF
  Char2 := 187; // 0xBB
  Char3 := 191; // 0xBF
  NewText := Char1 + Char2 + Char3 + OldText;
end;