Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 784401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:42:27+00:00 2026-05-14T20:42:27+00:00

I have a problem when attempting to serialize class on a server, send it

  • 0

I have a problem when attempting to serialize class on a server, send it to the client and deserialize is on the destination.

On the server I have the following two classes:

[XmlRoot("StatusUpdate")]
public class GameStatusUpdate
{
    public GameStatusUpdate()
    {}

    public GameStatusUpdate(Player[] players, Command command)
    {
        this.Players = players;
        this.Update = command;
    }

    [XmlArray("Players")]
    public Player[] Players { get; set; }

    [XmlElement("Command")]
    public Command Update { get; set; }
}

and

[XmlRoot("Player")]
public class Player
{
    public Player()
    {}

    public Player(PlayerColors color)
    {
        Color = color;
        ...
    }

    [XmlAttribute("Color")]
    public PlayerColors Color { get; set; }

    [XmlAttribute("X")]
    public int X { get; set; }

    [XmlAttribute("Y")]
    public int Y { get; set; }
}

(The missing types are all enums).

This generates the following XML on serialization:

<?xml version="1.0" encoding="utf-16"?>
<StatusUpdate>
  <Players>
    <Player Color="Cyan" X="67" Y="32" />
  </Players>
  <Command>StartGame</Command>
</StatusUpdate>

On the client side, I’m attempting to deserialize that into following classes:

[XmlRoot("StatusUpdate")]
public class StatusUpdate
{
    public StatusUpdate()
    {

    }

    [XmlArray("Players")]
    [XmlArrayItem("Player")]
    public PlayerInfo[] Players { get; set; }

    [XmlElement("Command")]
    public Command Update { get; set; }
}

and

[XmlRoot("Player")]
public class PlayerInfo
{
    public PlayerInfo()
    {
    }

    [XmlAttribute("X")]
    public int X { get; set; }

    [XmlAttribute("Y")]
    public int Y { get; set; }

    [XmlAttribute("Color")]
    public PlayerColor Color { get; set; }
}

However, the deserializer throws an exception:

There is an error in XML document (2, 2).
<StatusUpdate xmlns=''> was not expected.

What am I missing or doing wrong?

EDIT:

On request I’m also adding code used to serialize and deserialize:

Server:

    public static byte[] SerializeObject(Object obj)
    {
        XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
        StringWriter writer = new StringWriter();

        // Clear pre-defined namespaces
        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
        xsn.Add("", "");

        xmlSerializer.Serialize(writer, obj, xsn);
        writer.Flush();

        // Send as little-endian UTF-16 string because the Serializer denotes XML as 
        // utf-18 which cannot be easly changed
        UnicodeEncoding encoder = new UnicodeEncoding(false, false);
        return encoder.GetBytes(writer.ToString());
    }

Client:

    public static object DeserializeXml(string xmlData, Type type)
    {
        XmlSerializer xmlSerializer = new XmlSerializer(type);


        StringReader reader = new StringReader(xmlData);
        object obj = xmlSerializer.Deserialize(reader);

        return obj;
    }

Deserialization is invoked with

StatusUpdate update = (StatusUpdate) Util.DeserializeXml(xmlData, typeof (StatusUpdate));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:42:28+00:00Added an answer on May 14, 2026 at 8:42 pm

    After alot of testing I have finally found an error. It was not an encoding problem, neither was the problem in the other code nor it was the missing namespace.

    The missing part was the annotation for type of objects in the array when deserializing.

    So I had to change my StatusUpdate class to

    [XmlRoot("StatusUpdate")]
    public class StatusUpdate
    {
        public StatusUpdate()
        {
    
        }
    
        [XmlArray("Players"), XmlArrayItem(ElementName = "Player", Type = typeof(PlayerInfo))]
        public PlayerInfo[] Players { get; set; }
    
        [XmlElement("Command")]
        public ServerCommand Update { get; set; }
    }
    

    and serialization started working perfectly.

    I hope that helps someone else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.