Enum value not correctly given on XmlSerialization over Sockets

C

Curious

Hi,

I have a similar class to the one shown below, with the main difference
that it has more properties.

Now I am using the enum to indicate what type of data current exists.
I am using XmlSerialization to transfer the data over Sockets
(TcpClient, TcpListener).

Now when data is arriving at destination, MessageType is always being
indicated as Number even if it is not a Number.

I tried to convert the order of Number and Text, and then MessageType
always indicated as being as Text.

Then I concluded that the first item in the enum is being set.
From what could the problem being caused, and how can I solve it?


Can someone help me out
Thanks in Advance



public enum MessageType {Number, Text};

[Serializable]
public class Transfer
{
private int num;
private string text;
private MessageType mtype;

public Transfer(){}

public Transfer(int number)
{
NumberMsg = number;
mtype = MessageType.Number;
}

public Transfer(string txt)
{
TextMsg = txt;
mtype = MessageType.Text;
}

public int NumberMsg
{
get
{
return num;
}
set
{
num = value;
}
}

public int TextMsg
{
get
{
return text;
}
set
{
text = value;
}
}

public MessageType TransferType
{
get
{
return mtype;
}
}
}


// sample code being used by server
TcpListener serverListener = new TcpListener(endPointSettings);
serverListener.Start();
TcpClient socket = serverListener.AcceptTcpClient();
Transfer transfer;
XmlSerializer ser = new XmlSerializer(typeof(Transfer));
transfer = (Transfer)ser.Deserialize(socket.GetStream());

if (transfer.TransferType == MessageType.Text)
{
Console.Writeline("Text");
}
else if (transfer.TransferType == MessageType.Number)
{
Console.Writeline("Number");
}
else
{
Console.Writeline("None");
}


// sample code used by client
TcpClient client = new TcpClient();
client.Connect(serverEndPoint);
Transfer t = new DistributionTransfer(5);

XmlSerializer ser = new XmlSerializer(typeof(Transfer));
Stream s = client.GetStream();
ser.Serialize(s, t);
s.Close();
 
M

Marc Gravell

This could be more to do with the fact that it doesn't have a setter;

XmlSerializer is property based, and demands a public getter and setter to
any property, otherwise it doesn't get serialized / deserialized. You might
be able to do something by implementing IXmlSerializable to do your own
serialization, but personally I'd just make it read/write...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top