我有一个XML文件,内容如下:
<Contracts>
<Contract EntryType="U" ID="401" GroupCode="1">
</Contract>
</Contracts>
我有一个包含合同列表的类
[XmlArray("Contracts")]
[XmlArrayItem("Contract", typeof(Contract))]
public List<Contract> Contracts { get; set; }
当我尝试对此进行反序列化时,出现了以下错误:
"反映属性“Contracts”时出现错误。"
反序列化代码:
XmlSerializer reader = new XmlSerializer(typeof(ContractPosting));
xml.Position = 0;
eContractXML = (Contract)reader.Deserialize(xml);
以下是这些类:
public partial class ContractPosting
{
[XmlArray("Contracts")]
[XmlArrayItem("Contract", typeof(Contract))]
public List<Contract> Contracts { get; set; }
}
public class Contract
{
[XmlAttribute(AttributeName = "ContractID")]
public System.Nullable<int> ContractID { get; set; }
[XmlAttribute(AttributeName= "PostingID")]
public string PostingID { get; set; }
public EntryTypeOptions? EntryType { get; set; }
}
Cannot serialize member 'ContractID' of type System.Nullable``1[System.Int32]. XmlAttribute/XmlText cannot be used to encode complex types.
- 这是一个重复的问题,可以在https://dev59.com/lnI95IYBdhLWcg3w-C9b找到解决方案。 - mellamokb