471,570 Members | 937 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

Enum value not correctly given on XmlSerialization over Sockets

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();

Feb 9 '06 #1
2 1681
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
Feb 9 '06 #2
I just added the setter the property, and problem was solved.

Thanks

Feb 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Glenn Venzke | last post: by
9 posts views Thread by AngleWyrm | last post: by
21 posts views Thread by Andreas Huber | last post: by
18 posts views Thread by Visual Systems AB \(Martin Arvidsson\) | last post: by
6 posts views Thread by Jason Larion | last post: by
2 posts views Thread by Martin Engelhardt | last post: by
7 posts views Thread by Harris | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.