I am creating a Xml file with the following code (the byte array returned by Serialize()
is written to a FileStream
later):
public byte[] Serialize()
{
using (var stream = new MemoryStream())
{
WriteXmlToStream(stream);
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
string resultString = reader.ReadToEnd();
return Encoding.UTF8.GetBytes(resultString);
}
}
}
private void WriteXmlToStream(MemoryStream stream)
{
var document =
new XDocument(
new XElement("Coleta",
new XElement("Operador", Operador),
new XElement("Sujeito", Sujeito),
new XElement("Início", DataHora.ToString(Constantes.FormatoDataHora)),
new XElement("Descrição", Descrição),
// and so on
)
)
);
document.Save(stream);
}
But when I open the saved file, the unicode characters are "wrong":
<?xml version="1.0" encoding="utf-8"?>
<Coleta>
<Operador>Nome do Operador do Sofware</Operador>
<Sujeito>Nome Paciente de Teste</Sujeito>
<InÃcio>2015-05-19T02:24:10.10Z</InÃcio>
<Descrição>Coleta de teste para validação do formato de arquivo.</Descrição>
<Sensores>
<SensorInfo>
<Sensor>
<Nome />
<PosiçãoAnatômica>NãoEspecificada</PosiçãoAnatômica>
<Canais>
<Canal>
<!-- and so on -->
So what am I not doing, or doing wrong, and how should I fix it? I always have a hard time understanding these encoding peculiarities.
As mentioned in the comments, it happens because file editors are not opening the generated file with the correct (utf-8) encoding.
So my question is: how should I force encoding to the file?
UPDATE: it seems like this answer might be relevant: