我正在尝试将 byte[] 数组转换为 WP 中的 XDocument。我有以下代码
private void btnDownload_Click(object sender, RoutedEventArgs e)
{
svc.DownloadFileCompleted += new EventHandler<DownloadFileCompletedEventArgs>(svc_DownloadFileCompleted);
svc.DownloadFileAsync("XMLFile1.xml");
}
void svc_DownloadFileCompleted(object sender, DownloadFileCompletedEventArgs e)
{
byte[] bytes = e.Result;
//string responseText = Encoding.UTF8.GetString(bytes,0,2048); // ASCII assumed
//XDocument respnseXml = XDocument.Load(responseText);
bytes = NullRemover(bytes);
using (MemoryStream stream = new MemoryStream(bytes))
{
XDocument document = XDocument.Load(stream);
}
}
private byte[] NullRemover(byte[] DataStream)
{
int i;
byte[] temp = new byte[DataStream.Length];
for (i = 0; i < DataStream.Length - 1; i++)
{
if (DataStream[i] == 0x00) break;
temp[i] = DataStream[i];
}
byte[] NullLessDataStream = new byte[i];
for (i = 0; i < NullLessDataStream.Length; i++)
{
NullLessDataStream[i] = temp[i];
}
return NullLessDataStream;
}
我创建了一个下载文件的网络服务。我的代码工作正常。它将 byte[] 转换为 XDocument 对象。我不确定这种方式是否正确。为什么需要使用MemoryStream?将byte[]转换为XDocument是否正确?还是有其他的转换方式?