XML和JSON序列化问题 [英] XML and JSON Serialization Issues

查看:141
本文介绍了XML和JSON序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


此处是问题的详细信息:

我在一个看起来像这样的对象上有一个Property:


公共类MyObject

{

       公共距离宽度{get; set;}

}

 

如果我在Distance对象上实现ISerializable,则如果传入数据为JSON格式,则它与DataContract Serializer一起使用时效果很好,但在出现以下消息时失败带有XML:

System.Runtime.Serialization.SerializationException:来自命名空间的元素单元不能将子内容反序列化为对象.请使用XmlNode []反序列化 这种XML模式. ---> System.Xml.XmlException:应从名称空间结束元素'Unit'.找到文字"IN".第1行,位置116.

System.Runtime.Serialization.SerializationException: Element Unit from namespace  cannot have child contents to be deserialized as an object. Please use XmlNode[] to deserialize this pattern of XML. ---> System.Xml.XmlException: End element 'Unit' from namespace '' expected. Found text 'IN'. Line 1, position 116.

传入的Width的XML节点如下所示(注意,传入数据上没有指定名称空间对象–我想要的是)


< Width>

        < Unit> IN</Unit>

        < Value> 20</Value>

</Width>

 

ISerializable接口似乎迫使我使用名称空间,因为它序列化出站XML,如下所示:

 

<Width xmlns:d4p1="http://schemas.datacontract.org/2004/07/Harmony.DL.Library.Architecture">

        <Value xmlns:d5p1="http://www.w3.org/2001/XMLSchema" i:type="d5p1:double">20</Value>

        <Unit xmlns:d5p1="http://www.w3.org/2001/XMLSchema" i:type="d5p1:string">IN</Unit>

</Width >

 

即使使用标记距离对象 [XmlRoot(Namespace =""))]

Even if I tag the distance object with [XmlRoot(Namespace="")] attribute it still forces a namespace on the outbound object, leading me to believe this missing namespace is what is causing the serialization to fail when the inbound XML does not specify namespace.

如果我使用 [DataContract(Namespace = "'')] 改为使用适当的数据成员的属性 ISerializable 在JSON和XML中.问题在于此特定对象(距离)不是唯一地序列化的 使用数据协定序列化程序,并且我不想将数据协定属性与XML序列化属性混合使用-因此,当入站时,如何使ISerializable实现在默认的DataContract序列化程序中同时适用于JSON和XML XML没有指定命名空间?

If I use a [DataContract(Namespace="")] Attribute with the appropriate data members instead of ISerializable, then everything works as expected in both JSON and XML. The problem is that this particular object (Distance) is not serialized exclusively using the data contract serializer, and I don’t want to mix Data Contract attributes with XML Serialization attributes – so how do I get the ISerializable implementation to work in the default DataContract Serializer for both JSON and XML when the inbound XML does not have a Namespace specified?

谢谢

Mike Mlachak

推荐答案



我正在尝试让熟悉此主题的人进一步研究这个问题.可能会有一些时间延迟.感谢您的耐心.

最好的问候,
彭爱美
Hi,

I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

Best Regards,
Amy Peng


这篇关于XML和JSON序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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