反序列化嵌套类时不期望xmlns ='' [英] xmlns='' was not expected when deserializing nested classes

查看:36
本文介绍了反序列化嵌套类时不期望xmlns =''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在服务器上序列化类,将其发送到客户端并在目标上反序列化时遇到问题.

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; }
}

[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).

这会在序列化时生成以下XML:

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; }
}

[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?

根据要求,我还将添加用于序列化和反序列化的代码:

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

服务器:

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

客户:

    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;
    }

反序列化使用

StatusUpdate update = (StatusUpdate) Util.DeserializeXml(xmlData, typeof (StatusUpdate));

推荐答案

经过大量测试,我终于找到了一个错误.这不是编码问题,其他代码中的问题也不是缺少名称空间.

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.

所以我不得不将我的 StatusUpdate 类更改为

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.

这篇关于反序列化嵌套类时不期望xmlns =''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆