反序列化嵌套类时,不期望xmlns =”
尝试在服务器上序列化类,将其发送到客户端并在目标上反序列化时,我遇到了问题。
在服务器上,我有以下两个类:
[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; } }
(缺少的类型都是枚举)。
这会在序列化时生成以下XML:
StartGame
在客户端,我试图将其反序列化为以下类:
[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; } }
但是,反序列化器会抛出exception:
There is an error in XML document (2, 2). was not expected.
我错过了什么或做错了什么?
编辑:
根据要求,我还添加了用于序列化和反序列化的代码:
服务器:
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));
经过大量测试后,我终于发现了一个错误。 这不是编码问题,其他代码中的问题也不是缺少的命名空间。
缺少的部分是反序列化时数组中对象类型的注释。
所以我不得不将StatusUpdate
类更改为
[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; } }
和序列化开始完美。
我希望能帮到别人。
这是为我解决的问题:
[System.Xml.Serialization.XmlRootAttribute("nodeName", Namespace = "http://somenamespace", IsNullable = false)]
在使用XmlSerializer
时,这实际上是非常不寻常的。 根元素应始终如下所示:
(更正: XmlSerializer
看起来在反序列化时似乎没有这些,但是它总是在序列化时添加它们,所以如果它们丢失了就会有些腥。)
第二次编辑:
我强烈怀疑您的问题与编码有关。 我不知道为什么你必须如此混乱序列化,不能只使用默认的UTF-8编码,但无论如何,以下代码可以正常工作:
MyClass m = new MyClass() { X = 4, Y = 8 }; byte[] data = SerializeObject(m); string xml = Encoding.Unicode.GetString(data); Console.WriteLine(xml); m = (MyClass)DeserializeXml(xml, typeof(MyClass));
因此,如果某些事情失败了,那么在客户端将字节数组转换为XML字符串时很可能会出错。 这是你还没有发布的唯一代码。
我得到了解决
XmlSerializer serializer = new XmlSerializer(typeof(CustomerPhoto), "http://example.com/webservices/"); CustomerPhoto returnObject = (CustomerPhoto)serializer.Deserialize(xmlStream);