我创建了一个类并将其转换为 xml。
问题是,当我将类 xml 字符串转换为字节
时,ASCII.GetBytes
返回一个字节数组,
在ascArray
它总是一个?字符,所以 xml 像这样开始
?<?xml version="1.0" encoding="utf-8"?>
为什么会这样?
这是代码:
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();