【发布时间】:2020-01-08 04:42:05
【问题描述】:
我有这个 XML 作为 API 的结果。使用 DotNetXmlDeserializer 作为反序列化器。
<results>
<result inum=\"802469000014312\">
<field name=\"startedat\">2019-09-04T15:59:56.372</field>
<field name=\"duration\">8</field>
<field name=\"otherparties\">1043 (DEMO BLA BLA), 0519331839</field>
<field name=\"switchcallid\">00001000081567605580</field>
<field name=\"udfs\"></field>
</result>
<result inum=\"802469000014313\">
<field name=\"startedat\">2019-09-04T16:00:31.414</field>
<field name=\"duration\">6</field>
<field name=\"otherparties\">1043 (DEMO BLA BLA), 0519331839</field>
<field name=\"switchcallid\">00001000091567605608</field>
<field name=\"udfs\"></field>
</result>
我正在使用这些类进行反序列化
[XmlRoot("results")]
public class WFOQueryDTO_List
{
public WFOQueryDTO_List() { Items = new List<WFOQueryDTO_Out>(); }
[XmlElement("result")]
public List<WFOQueryDTO_Out> Items { get; set; }
}
public class WFOQueryDTO_Out
{
[XmlElement("field")]
[XmlAttribute("startedat")]
public DateTime startedat { get; set; }
[XmlElement("field")]
[XmlElement("duration")]
public int duration { get; set; }
[XmlElement("field")]
[XmlElement("otherparties")]
public string otherparties { get; set; }
[XmlElement("field")]
[XmlElement("switchcallid")]
public string switchcallid { get; set; }
[XmlElement("field")]
[XmlElement("udfs")]
public string udfs { get; set; }
}
但我明白了:
System.InvalidOperationException: There was an error reflecting type ...For non-array types, you may use the following attributes: XmlAttribute, XmlText, XmlElement, or XmlAnyElement.
【问题讨论】:
-
字段元素需要私有和公共属性。所以你需要一个get/set接口。简单。将尽快发布答案
标签: c# .net xml serialization