Why do I get an extra character (dot or marker) at the beginning of my byte array?

I have the following code used to get xml from a DataSet into a byte array using UTF-8 encoding:

private static byte[] fGetXmlBytes(DataTable lvDataTable)
{
  XmlWriterSettings lvSettings = new XmlWriterSettings();
  lvSettings.Encoding = Encoding.UTF8;
  lvSettings.NewLineHandling = NewLineHandling.Replace;
  lvSettings.NewLineChars = String.Empty;

  using(MemoryStream lvMemoryStream = new MemoryStream())
  using (XmlWriter lvWriter = XmlWriter.Create(lvMemoryStream, lvSettings))
  {
    lvDataTable.WriteXml(lvWriter, XmlWriteMode.IgnoreSchema);
    //Lines used during debugging
    //byte[] lvXmlBytes = lvMemoryStream.GetBuffer();
    //String lsXml = Encoding.UTF8.GetString(lvXmlBytes, 0, lvXmlBytes.Length);
    return lvMemoryStream.GetBuffer();
  }
}

I need an array of bytes, because later on I pass data to compression and encryption routines that work on byte arrays. The problem is that I end the extra character at the beginning of xml. Instead:

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

I get

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

Does anyone know why the character is? Is there a way to prevent adding a character? Or is it easy to take it off?

Colin

+3
source share
4 answers

Encoding, . , Encoding.UTF8, , UTF8Encoding, , :

lvSettings.Encoding = new UTF8Encoding(false);

UTF-8 - UNICODE (U + FEFF), UTF-8. UNICODE , ( ) 16- . 0xEF 0xFF, endian; , 0xFF 0xEF, .

U + FEFF, UTF-8, 0xEF 0xBB 0xBF , UTF-8 8- , .

+13

- UTF-8. AFAIK . ? XML, , .

0

, :


MemoryStream data = new MemoryStream(1000);
datatable.WriteXml(data);
return data.toArray();
0

Source: https://habr.com/ru/post/1714879/


All Articles