为什么ASCII.GetBytes返回错误的字节

时间:2012-06-14 05:25:40

标签: c# xml encoding ascii

我创建一个类并将其转换为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();

1 个答案:

答案 0 :(得分:4)

我强烈怀疑数据以byte order mark开头,无法用ASCII表示。

目前尚不清楚你为什么要做你正在做的事情,特别是在MemoryStream附近。为什么要创建一个UTF-8编码的字节数组,然后将其解码为字符串(我们不知道UTF8ByteArrayToString做了什么),然后将它返回转换为字节数组?为什么不直接将字节数组写入客户端开始?如果您需要将数据作为字符串,我将使用StringWriter的子类,该子类通告它使用UTF-8作为编码。如果需要它作为字符串,只需坚持字节数组。

请注意,即使除了第一个字符外,您还有一个以UTF-8编码的XML文档,这意味着字符串中可能还有其他非ASCII字符。你为什么一直在使用ASCII?

编辑:为了清楚起见,你从根本上应用了一个有损转换,不必要地做了。即使您想要数据的本地副本,也应该具有以下内容:

// Removed bad try/catch block - don't just catch Exception, and don't
// just swallow exceptions
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(WorkItem));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, p);

// Removed pointless conversion to/from string
// Removed pointless BinaryWriter (just use the stream)

// An alternative would be memoryStream.WriteTo(clientStream);
byte[] data = memoryStream.ToArray();
clientStream.Write(data, 0, data.Length);
Console.WriteLine(" send.." + data);

// Removed Close calls - you should use "using" statements to dispose of
// streams automatically.