Im creating a class and converting it into xml.
The problem is that when i convert the class xml string into bytes
the ASCII.GetBytes return a byte array with
an extra character in the beginning of the ascArray
It's always a ? character so the xml starts like this
?<?xml version="1.0" encoding="utf-8"?>
Why is this happening?
This is the code:
WorkItem p = new WorkItem();
// Fill the class with whatever need to be sent to client
OneItem posts1 = new OneItem();
posts1.id = "id 1";
posts1.username = "hasse";
posts1.message = "hej again";
posts1.time = "time1";
p.setPost(posts1);
OneItem posts2 = new OneItem();
posts2.id = "id 2";
posts2.username = "bella";
posts2.message = "hej again again";
posts2.time = "time2";
p.setPost(posts2);
// convert the class WorkItem to xml
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(WorkItem));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, p);
// send the xml version of WorkItem to client
byte[] data = memoryStream.ToArray();
clientStream.Write(data, 0, data.Length);
Console.WriteLine(" send.." + data);
clientStream.Close();
XmlizedStringlook like if you breakpoint atByte[] ascArray= Encoding.ASCII.GetBytes(XmlizedString);UTF8ByteArray. I'm pretty sure you'll find it starts with 0xEF, 0xBB, 0xBF, which is the UTF-8 representation of the byte order mark. That's being decoded byUTF8ByteArrayToStringinto a single character (not byte) - but that character can't be represented in ASCII. Fundamentally, you're applying a lossy transformation here.