简体   繁体   English

XML 反序列化不适用于抽象类

[英]XML Deserialization doesn't work with abstract class

I'm trying to deserialize from a xml string to an object.我正在尝试将 xml 字符串反序列化为对象。 But my obect is always null.但我的对象始终为空。

I have an abstract class (Response), a class that inherits from "Response" (DirectorSearchResponse), and an object in the class "DirectorSearchResponse" (HeaderResponse).我有一个抽象类(Response),一个继承自“Response”(DirectorSearchResponse)的类,以及“DirectorSearchResponse”(HeaderResponse)类中的一个对象。 This object is always null after deserialization.反序列化后,此对象始终为空。

Response.cs响应文件

public abstract class Response
{
    public HeaderResponse Header { get; set; }

    public Response()
    {
    }
}

DirectorSearchResponse.cs DirectorSearchResponse.cs

[XmlRoot("xmlresponse")]
public class DirectorSearchResponse : Response
{
    public DirectorSearchResponse() : base()
    {
        /* DO NOTHING */
    }
}

HeaderResponse.cs HeaderResponse.cs

[XmlRoot("header")]
public class HeaderResponse
{
    [XmlElement("toto")]
    public String toto { get; set; }

    public HeaderResponse()
    {

    }
}

My running code :我的运行代码:

        /* DESERIALIZE */
        String toto = "<xmlresponse><header><toto>tutu</toto><reportinformation><time>08/04/2016 13:33:37</time><reporttype> Error</reporttype><country>FR</country><version>1.0</version><provider>www.creditsafe.fr</provider><chargereference></chargereference></reportinformation></header><body><errors><errordetail><code>110</code><desc></desc></errordetail></errors></body></xmlresponse>";
        XmlSerializer xsOut = new XmlSerializer(typeof(DirectorSearchResponse));
        using (TextReader srr = new StringReader(toto))
        {
            DirectorSearchResponse titi = (DirectorSearchResponse)xsOut.Deserialize(srr);
        }

When I execute my code, the object "titi" is instanciate, but "Header" is always null.当我执行我的代码时,对象“titi”是实例化的,但“Header”始终为空。

How retrieve the "toto" value from xml ?如何从 xml 检索“toto”值?

XML is case sensitive, so you need to use [XmlElement("header")] to inform the serializer of the correct element name for the Header property: XML 区分大小写,因此您需要使用[XmlElement("header")]通知序列化程序Header属性的正确元素名称:

public abstract class Response
{
    [XmlElement("header")]
    public HeaderResponse Header { get; set; }

    public Response()
    {
    }
}

The [XmlRoot("header")] you have applied to HeaderResponse only controls its element name when it is the root element of an XML document.您已应用于HeaderResponse[XmlRoot("header")]仅在它是 XML 文档的根元素时控制其元素名称。

You need to add the link to the abstract class like this :您需要像这样将链接添加到抽象类:

[XmlRoot(ElementName = "Response")]
public abstract class Response
{
     public HeaderResponse Header { get; set; }

    public Response()
    {
    }
}

[XmlRoot(ElementName = "Response")]
public class DirectorSearchResponse : Response
{
    public DirectorSearchResponse() : base()
   {
    /* DO NOTHING */
   }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM