Tengo un problema al intentar serializar la clase en un servidor, enviarla al cliente y deserializar en el destino.xmlns = '' no se esperaba al deserializar las clases anidadas
En el servidor que tengo las dos clases siguientes:
[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; }
}
y
[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; }
}
(Los tipos que faltan son todas las enumeraciones).
Esto genera el siguiente código XML en la serialización:
<?xml version="1.0" encoding="utf-16"?>
<StatusUpdate>
<Players>
<Player Color="Cyan" X="67" Y="32" />
</Players>
<Command>StartGame</Command>
</StatusUpdate>
En el lado del cliente, estoy tratando de deserializar que en las clases siguientes:
[XmlRoot("StatusUpdate")]
public class StatusUpdate
{
public StatusUpdate()
{
}
[XmlArray("Players")]
[XmlArrayItem("Player")]
public PlayerInfo[] Players { get; set; }
[XmlElement("Command")]
public Command Update { get; set; }
}
y
[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; }
}
Sin embargo, el deserializador arroja una excepción:
There is an error in XML document (2, 2).
<StatusUpdate xmlns=''> was not expected.
¿Qué me estoy perdiendo o haciendo mal?
EDIT:
A petición también estoy añadiendo código utilizado para serializar y deserializar:
Servidor:
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());
}
Cliente:
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;
}
Deserialización se invoca con
StatusUpdate update = (StatusUpdate) Util.DeserializeXml(xmlData, typeof (StatusUpdate));
La primera cosa que se está perdiendo no nos está mostrando el código que está utilizando para deserializar. –
Esto todavía está incompleto. ¿De dónde viene la cadena 'xmlData' cuando se serializa como bytes en el otro extremo? No me sorprendería si esto es realmente un problema de codificación. ¿Y por qué su comentario menciona "utf-18" cuando no existe tal cosa? – Aaronaught
En mi caso, fue por una declaración incorrecta de 'XmlSerializer'. Así que revisa eso también. – Mangesh