我正在尝试将字节数组转换为数据表。当我尝试反序列化时,我得到一个错误"输入流不是有效的二进制格式"。
有谁知道如何解决这个问题?
这就是我将原始数据文件转换为字节数组的方式;
FileStream fStream = new FileStream(dirName + filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
byte[] data = br.ReadBytes((int)numBytes);
br.Close();
var request = (HttpWebRequest)HttpWebRequest.Create(endpoint);
request.Accept = "application/json";
request.ContentType = "application/json";
request.Method = method;
var serializer2 = new DataContractJsonSerializer(typeof(byte[]));
var requestStream = request.GetRequestStream();
serializer2.WriteObject(requestStream, data);
requestStream.Close();
var response2 = request.GetResponse();
这就是我尝试转换为数据表的方式。
public string transferData(byte[] f)
{
DataTable dt = new DataTable();
DataSet tempDataSet = new DataSet();
// Deserializing into datatable
using (MemoryStream stream = new MemoryStream(f))
{
BinaryFormatter bformatter = new BinaryFormatter();
dt = (DataTable)bformatter.Deserialize(stream);
if (dt != null)
{
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("----------------------");
Console.WriteLine("PostalCode:" + row["POSTAL_CODE"]);
Console.WriteLine("CountryCode:" + row["COUNTRY_CODE"]);
Console.WriteLine("ProcessNumber:" + row["PROCESS_NUMBER"]);
Console.WriteLine("ProcessStatus:" + row["PROCESS_STATUS"]);
Console.WriteLine("TimeStamp:" + row["TIMESTAMP"]);
Console.WriteLine("----------------------");
}
}
}
// Adding DataTable into DataSet
tempDataSet.Tables.Add(dt);
错误 {"输入流不是有效的二进制格式。起始内容(以字节为单位)为:54-2C-41-2C-39-31-37-2C-39-32-32-30-30-2C-46-31-2C ......"}
。所以现在我试图像使用json一样反序列化...
MemoryStream stream = new MemoryStream(f);
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(byte[]));
stream.Position = 0;
var x1 = (byte[])ser.ReadObject(stream);
新的错误讯息 类型' System.Runtime.Serialization.SerializationException'的例外情况发生在System.Runtime.Serialization.dll中但未在用户代码中处理
其他信息:反序列化System.Byte []类型的对象时出错。遇到意外的角色' T'。