C# 反序列化列表计数为零

标签 c# xml serialization deserialization

让我的类正确反序列化时遇到一些问题。

其他反序列化类工作正常,性质相似,但当反序列化器运行时,ErrorDetail 和 String 列表的计数均为 0。

我错过了什么/做错了什么?

XML:

<PlaceOrderResponse
    xmlns="http://blah.co.uk">
    <PlaceOrderResult
        xmlns:a="http://blah.co.uk/WebserviceMessage"
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:ErrorDetails
            xmlns:b="http://blah.co.uk/Error">
            <b:ErrorDetail>
                <b:Code>1fasd</b:Code>
                <b:Message>Explain</b:Message>
            </b:ErrorDetail>
        </a:ErrorDetails>
        <a:ID i:nil="true"/>
        <a:InformationalMessages
            xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
            <a:Status>1</a:Status>
        </PlaceOrderResult>
    </PlaceOrderResponse>

C# 类:

[Serializable()]
    [XmlRoot(ElementName = "PlaceOrderResponse", Namespace = "http://blah.co.uk", IsNullable = false)]
    [XmlType(AnonymousType = true, Namespace = "http://blah.co.uk")]
    public class PlaceOrderResponse
    {
        [XmlElement(ElementName = "PlaceOrderResult")]
        public PlaceOrderResult placeOrderResult { get; set; }
    }

    [XmlRoot(ElementName = "PlaceOrderResult")]
    public class PlaceOrderResult : WebserviceMessage { }

[XmlRoot(Namespace = "http://blah.co.uk/WebserviceMessage")]
    [XmlType(AnonymousType = true)]
    public class WebserviceMessage
    {
        [XmlArray("ErrorDetails")]
        [XmlArrayItem("ErrorDetail", typeof(ErrorDetail))]
        public ErrorDetails errorDetails { get; set; }
        [XmlElement("ID")]
        public string ID { get; set; }
        [XmlArray("InformationMessages")]
        [XmlArrayItem(typeof(String))]
        public List<String> InformationMessages { get; set; }
        [XmlElement("Status")]
        public string Status { get; set; }
    }

[XmlRoot(ElementName = "ErrorDetails")]
    [XmlTypeAttribute(AnonymousType = true)]
    public class ErrorDetails : List<ErrorDetail> { }

    [XmlRoot(ElementName = "ErrorDetail", Namespace = "http://blah.co.uk/Error")]
    [XmlTypeAttribute(AnonymousType = true)]
    public class ErrorDetail
    {
        [XmlElement(ElementName = "Code")]
        public string Code { get; set; }
        [XmlElement(ElementName = "Message")]
        public string Message { get; set; }
    }

最佳答案

您的XmlArrayItem将从其父级继承其 namespace 。因此,您需要明确包含此内容:

[XmlArrayItem("ErrorDetail", typeof(ErrorDetail), Namespace = "http://blah.co.uk/Error")].

关于信息性消息,没有要加载的字符串,因此很难判断 XML 应该是什么样子。它将需要 <string>message</string> 形式的元素

作为提示,调试这些问题的最简单方法是尝试相反的方法 - 创建一个对象并将其序列化为 XML。然后将其与您尝试反序列化的内容进行比较,您会发现很容易发现差异所在。

关于C# 反序列化列表计数为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34660922/

相关文章:

c# - 单击主应用程序窗口时如何聚焦模态 WPF 窗口

c# - 如何序列化 Azure 服务总线中的异常?

python - 类型对象 'Users' 没有属性 '_meta'

c# - 如何配置 protobuf-net 的 RuntimeModel.Default 以支持序列化/反序列化 SessionSecurityToken?

c# - 使用 C# 在字符串中查找具有频率的子字符串的最佳方法是什么?

c# - 如何在 C#.NET 中提供应用程序路径?

javascript - javascript处理xsl时的document()方法问题

PHP SimpleXML 缺少属性

c# - 执行任务XamlCTask- Xamarin时出错

php - PHP 和 MySQL 的大小写敏感问题