Have you tried constructing a StreamReader object (passing in the Stream object + Encoding into the StreamReader constructor)
then calling XDocument.Load(StreamReader) to create the document?


On 15/04/2012 6:26 PM, Greg Keogh wrote:

Folks, I have an XML file as an embedded resource. The file looks like this and I know it's utf-8 encoded because in the binary editor I can see it starts with 0xEFBBBF....

<?xmlversion="1.0"encoding="utf-8"?>

<root>

<stuff>Foobar</stuff>

</root>

However, when I run the following code to get the raw bytes from the resource and convert it to a string I don't get the nice string I expect. The resulting string starts with the character 0xfeff which is a Unicode BOM and then XDocument dies attempting to parse that string.

using(Stream s = typeof(Program).Assembly.GetManifestResourceStream("MyProgram.XMLFile1.xml"))

{

byte[] buff = new byte[s.Length];

    s.Read(buff, 0, buff.Length);

string xml = Encoding.UTF8.GetString(buff, 0, buff.Length);

XDocument doc2 = XDocument.Parse(xml);

// This dies because xml[0] is the character 0xfeff

}

I've tried different overloads of Encoding classes but it makes no difference. I could have sworn I was a boffin of bytes and encoding, but I've no idea what's going on here. Any ideas anyone?

Greg


Reply via email to