views:

205

answers:

3

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?"

+2  A: 

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

Jon Skeet
+1  A: 

The editor is placing the well known unicode character marker known as the BOM (byte order mark) at the start of the file. This is used to show what the correct unicode encoding of the file is using - in this case it is UTF-8, but depending on the actual encoding the byte values will be different.

1800 INFORMATION
+1  A: 

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.

peter.murray.rust
It does try to parse the BOM. In fact, it uses it to determine the correct type of encoding of the stream, which is what it is meant for.
Abel