我有一个XML,内容是
<Contracts>
<Contract EntryType="U" ID="401" GroupCode="1">
</Contract>
</Contracts>
我有一个包含合同列表的课程
[XmlArray("Contracts")]
[XmlArrayItem("Contract", typeof(Contract))]
public List<Contract> Contracts { get; set; }
所以当我尝试反序列化时,我收到了这个错误:
“反映财产'合同'的错误。”
反序列化代码:
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; }
}
答案 0 :(得分:0)
由于根节点为<Contracts>
,请尝试重新安排您的课程:
[XmlRoot("Contracts")]
public class ContractPosting {
[XmlElement("Contract", typeof(Contract))]
public List<Contract> Contracts { get; set; }
}
当您使用XmlArray
和XmlArrayItem
时,它们必须嵌套在某些内容中。但是,您当前的XmlArray
标记实际上是XML
文件的根节点,因此它必须是XmlRoot
。
答案 1 :(得分:0)
谢谢,问题是Nullable类型,我以这种方式解决了
[XmlIgnore]
public System.Nullable<int> ContractID { get; set; }
[XmlAttribute("ContractID")]
public int ContractIDxml {
get { return ContractID ?? 00; }
set { ContractID = value; }
}