473,791 Members | 3,211 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 1766
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
4851
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
4607
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
11371
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
4619
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
7548
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
9837
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
0
9512
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
10419
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10147
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
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9023
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
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2910
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.