Hi,
I want to serialize a class and send it over tcp. Thats no problem, but I get an exception while deserializing the stream in the client application.
Thats my code:
[Serializable]
internal class SendObj1
{
public int cmd;
public string msg;
}
public class SendObj
{
public int cmd;
public string msg;
public byte[] ToByte(int Command)
{
SendObj1 obj1 = new SendObj1();
obj1.cmd = Command;
XmlSerializer xs = new XmlSerializer(typeof(SendObj1));
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, obj1);
return ms.ToArray();
}
public byte[] ToByte(int Command, string Message)
{
SendObj1 obj1 = new SendObj1();
obj1.cmd = Command;
obj1.msg = Message;
XmlSerializer xs = new XmlSerializer(typeof(SendObj1));
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, obj1);
return ms.ToArray();
}
public SendObj ToObj(byte[] buffer)
{
SendObj1 obj = new SendObj1();
try
{
XmlSerializer xs = new XmlSerializer(typeof(SendObj1));
MemoryStream ms = new MemoryStream();
obj = (SendObj1) xs.Deserialize(ms);
msg = obj.msg;
cmd = obj.cmd;
}
catch (Exception e)
{
}
return this;
}