473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1748
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
9
by: AngleWyrm | last post by:
"The C++ Programming Language" by Bjarne Stroustrup, copyright 1997 by AT&T, section 4.8 (pp 77): "A value of integral type may be explicitly converted to an enumeration type. The result of such a...
1
by: dpakpaul | last post by:
Hi, I have an XSD schema where I have attributes that are declared to contain non string values such as integers etc. Take for example, this declaration - <xs:attribute name="IsThisTrue"...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
6
by: Jason Larion | last post by:
When working with enums, I've noticed some behaviour that seems completely counter-intuitive to me. I was wondering if someone here could help restore my sanity, or at least help me to understand...
1
by: martin | last post by:
This code compiles (this is how it should work): Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); This code does not compile (passing an int instead of...
2
by: Martin Engelhardt | last post by:
Hi, I want to use the following: Class Test ... Public Enum TestEnum as integer Enum1 = 128 Enum2 = 256 End Enum
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.