Enum value not correctly given on XmlSerialization over Sockets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Curious

    #1

    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 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.
    [color=blue]
    >From what could the problem being caused, and how can I solve it?[/color]


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

  • Marc Gravell

    #2
    Re: Enum value not correctly given on XmlSerializatio n over Sockets

    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


    Comment

    • Curious

      #3
      Re: Enum value not correctly given on XmlSerializatio n over Sockets

      I just added the setter the property, and problem was solved.

      Thanks

      Comment

      Working...