我創建一個類並將其轉換爲xml。爲什麼ASCII.GetBytes返回錯誤的字節
的問題是,當我的類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();
如何輸入'XmlizedString'看,如果你斷點'字節] ascArray = Encoding.ASCII.GetBytes(XmlizedString像);' –
這很奇怪,它有領先的?也只是如果我複製/粘貼到XmlizedString的內容纔可見 – Erik
那麼爲什麼UTF8ByteArrayToString增加一個額外的字節 – Erik