【发布时间】:2016-04-08 15:44:04
【问题描述】:
我正在尝试将 xml 字符串反序列化为对象。但我的对象始终为空。
我有一个抽象类(Response),一个继承自“Response”的类(DirectorSearchResponse),以及一个“DirectorSearchResponse”类中的对象(HeaderResponse)。该对象在反序列化后始终为空。
响应.cs
public abstract class Response
{
public HeaderResponse Header { get; set; }
public Response()
{
}
}
DirectorSearchResponse.cs
[XmlRoot("xmlresponse")]
public class DirectorSearchResponse : Response
{
public DirectorSearchResponse() : base()
{
/* DO NOTHING */
}
}
HeaderResponse.cs
[XmlRoot("header")]
public class HeaderResponse
{
[XmlElement("toto")]
public String toto { get; set; }
public HeaderResponse()
{
}
}
我的运行代码:
/* 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);
}
当我执行我的代码时,对象“titi”是实例化的,但“Header”始终为空。
如何从 xml 中检索“toto”值?
【问题讨论】:
标签: c# xml deserialization xmlserializer