Converter BYtes em Object
Boas Pessoal
Eu tenho um projeto aqui que esotu me conectando via socket com outro, estou pegando a respostas em Bytes e estou tentando converter em outra coisa (objeto) mas nao estou conseguindo, alguem tem uma dica ?
Segue treço do codigo.
//pega bytes da entrada
byte[] entrada = new byte[TAMANHO_BUFFER];
// Recebe uma mensagem do servidor
var retorno = servidorStream.Read(entrada, 0, (int)this.cliente.ReceiveBufferSize);
if (retorno != null)
{
try
{
MemoryStream stream = new MemoryStream(entrada);
// convert stream to string
StreamReader reader = new StreamReader(stream);
System.Xml.Serialization.XmlSerializer reply = new System.Xml.Serialization.XmlSerializer(typeof(Dao.imagereply));
imageReply = (imagereply)reply.Deserialize(reader);
// var grid = (reader.ReadToEnd() as imagereply);
// imageReply = (imagereply)
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}
}
Quando vai ocorrer:
System.Xml.Serialization.XmlSerializer reply = new System.Xml.Serialization.XmlSerializer(typeof(Dao.imagereply)); imageReply = (imagereply)reply.Deserialize(reader);
esta dando erro, pelo que vi ele ta tentando desseralizar bytes direto ai diz que não é tipo XMLSERIALIZE
Descobri que Não é um XML e objeto que contem parte de XML estou tentandoe sse codigo agora:
try
{
// convert byte array to memory stream
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(entrada);
// create new BinaryFormatter
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// set memory stream position to starting point
_MemoryStream.Position = 0;
// Deserializes a stream into an object graph and return as a object.
object comoficou = _BinaryFormatter.Deserialize(_MemoryStream);
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}
Esta retornando o erro:
{"The input stream is not a valid binary format. The starting contents (in bytes) are: 49-4D-41-47-45-58-4D-4C-20-20-20-20-20-20-20-20-20 ..."}
Discussão (1)
Carregando comentários...