我有这样的xml
<?xml version="1.0" ?>
<response>
<id-number>2979183</id-number>
<differentiator-question>
<prompt>How old are you?</prompt>
<type>age.range</type>
<answer>29 - 38</answer>
<answer>39 - 48</answer>
<answer>49 - 58</answer>
<answer>59 - 68</answer>
<answer>None of the above</answer>
</differentiator-question>
<error>Invalid username and password</error>
</response>
我尝试使用.NET中的内置序列化机制反序列化。 创建了这样一个类
[XmlRoot("response")]
public class IdologyAnswerMapping
{
[XmlElement("id-number")]
public string IdNumber { get; set; }
public class DifferentiatorQuestionType
{
[XmlElement("prompt")]
public string Prompt { get; set; }
[XmlElement("type")]
public string Type { get; set; }
[XmlArray("answer")]
public string[] Answers { get; set; }
}
[XmlElement("differentiator-question")]
public DifferentiatorQuestionType DifferentiatorQuestion { get; set; }
public static IdologyAnswerMapping FromXml(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(IdologyAnswerMapping));
IdologyAnswerMapping answer = (IdologyAnswerMapping)serializer.Deserialize(new StringReader(xml));
return answer;
}
}
但Answers数组的大小为零。如何将所有<answer />
标记放入Answers数组?
谢谢。
答案 0 :(得分:1)
使用XmlElement
代替XmlArray
[XmlElement("answer")]
public string[] Answers { get; set; }