Note that there are some explanatory texts on larger screens.

plurals
  1. POxmlns='' was not expected when deserializing nested classes
    primarykey
    data
    text
    <p>I have a problem when attempting to serialize class on a server, send it to the client and deserialize is on the destination.</p> <p>On the server I have the following two classes:</p> <pre><code>[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; } } </code></pre> <p>and</p> <pre><code>[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; } } </code></pre> <p>(The missing types are all enums).</p> <p>This generates the following XML on serialization:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt; &lt;StatusUpdate&gt; &lt;Players&gt; &lt;Player Color="Cyan" X="67" Y="32" /&gt; &lt;/Players&gt; &lt;Command&gt;StartGame&lt;/Command&gt; &lt;/StatusUpdate&gt; </code></pre> <p>On the client side, I'm attempting to deserialize that into following classes:</p> <pre><code>[XmlRoot("StatusUpdate")] public class StatusUpdate { public StatusUpdate() { } [XmlArray("Players")] [XmlArrayItem("Player")] public PlayerInfo[] Players { get; set; } [XmlElement("Command")] public Command Update { get; set; } } </code></pre> <p>and</p> <pre><code>[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; } } </code></pre> <p>However, the deserializer throws an exception:</p> <pre><code>There is an error in XML document (2, 2). &lt;StatusUpdate xmlns=''&gt; was not expected. </code></pre> <p>What am I missing or doing wrong?</p> <p>EDIT:</p> <p>On request I'm also adding code used to serialize and deserialize:</p> <p>Server:</p> <pre><code> 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()); } </code></pre> <p>Client:</p> <pre><code> 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; } </code></pre> <p>Deserialization is invoked with</p> <pre><code>StatusUpdate update = (StatusUpdate) Util.DeserializeXml(xmlData, typeof (StatusUpdate)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload