473,804 Members | 3,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enum value not correctly given on XmlSerializatio n 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 XmlSerializatio n 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.Num ber;
}

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

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(end PointSettings);
serverListener. Start();
TcpClient socket = serverListener. AcceptTcpClient ();
Transfer transfer;
XmlSerializer ser = new XmlSerializer(t ypeof(Transfer) );
transfer = (Transfer)ser.D eserialize(sock et.GetStream()) ;

if (transfer.Trans ferType == MessageType.Tex t)
{
Console.Writeli ne("Text");
}
else if (transfer.Trans ferType == MessageType.Num ber)
{
Console.Writeli ne("Number");
}
else
{
Console.Writeli ne("None");
}
// sample code used by client
TcpClient client = new TcpClient();
client.Connect( serverEndPoint) ;
Transfer t = new DistributionTra nsfer(5);

XmlSerializer ser = new XmlSerializer(t ypeof(Transfer) );
Stream s = client.GetStrea m();
ser.Serialize(s , t);
s.Close();

Feb 9 '06 #1
2 1767
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 IXmlSerializabl e 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
4852
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 EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
9
3141
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 conversion is undefined unless the value is within the range of the enumeration. For example: enum flag { x=1, y=2, z=4, e=8 }; // range 0:15 flag f1 = 5; // type error: 5 is not of type flag flag f2 = flag(5); // ok: flag(5) is of type flag and...
1
4142
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" type="xs:boolean" /> Now, I want to write or modify an XML document using the DOM API (using C#) where this attribute will be given a boolean value. Then the resulting XML document will be validated against the schema
21
4609
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
11375
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
4620
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 the "why" of the behaviour. After dimensioning an enum of type integer, any attribute referenced seems to, by default, return the name of that attribute as a string, instead of the integer value assigned to it. The code snippet that follows...
1
1788
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 an AddressFamily is bad): Socket sock = new Socket(1, SocketType.Stream, ProtocolType.Tcp); But why on earth does not compile: Socket sock = new Socket(0, SocketType.Stream, ProtocolType.Tcp);
2
7549
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
9838
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10318
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.