Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid reading the byte order mark (BOM) in a Resources file in Visual Studio?

Tags:

c#

xml

resources

I am trying to use the Visual Studio editor to create XML files in the Resources area of an Assembly in C#. The files appear perfectly correct in the XML editor and honour my schema (recognising the elements and attributes). However when I try to read them (from the Resources) they fail because they consistently have 3 spurious characters at the start of the file ( or #EF #BB #BF).

These characters do NOT appear in the editor but they are there in an external binary editor. When I remove them manualy the files behave properly.

What can I do to create XML files reliably in the Resources area?

After first 2 replies I modified the question to

"How do I read a resources file to avoid including the byte order mark?"

like image 678
peter.murray.rust Avatar asked Dec 14 '22 03:12

peter.murray.rust


2 Answers

The XML editor creates an XML file by default with the encoding UTF-8 and adds the XML declaration:

<?xml version="1.0" encoding="utf-8" ?>

Presumably it also adds the encoding (which in UTF-8 is 3 bytes as above). The following method (found by a friend) seems to read the bytes without having to know the encoding:

String ss = new StreamReader( new MemoryStream(bytes), true ).ReadToEnd();

and this now does not try to parse the BOM as part of content.

like image 76
peter.murray.rust Avatar answered Dec 16 '22 18:12

peter.murray.rust


They're not spurious. They're the byte order mark indicating UTF-8.

like image 23
Jon Skeet Avatar answered Dec 16 '22 18:12

Jon Skeet