反序列化列表错误

4

我有一个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; }
} 

3
如果查看“InnerException”,您将获得更详细的错误信息,解释为什么无法反序列化。 - mellamokb
1
你能把类的定义粘贴进来吗?public class Contract {}...等等。 - A-Dubb
错误出在“Contract”类中。请编辑您的问题以显示此类和内部异常。 - Richard Schneider
在 System.Xml.Serialization.XmlReflectionImporter 中导入类型映射(TypeModel model,String ns,ImportContext context,String dataType,XmlAttributes a,Boolean repeats,Boolean openModel,RecursionLimiter limiter)的方法: 目标站点:{Boolean InitializeStructMembers(System.Xml.Serialization.StructMapping, System.Xml.Serialization.StructModel, Boolean, System.String, System.Xml.Serialization.RecursionLimiter)} - José Arthur Ortiz Antunes
2
我测试了你的代码,当我深入到最底层的InnerException(4级)时,我发现了这个错误信息: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
显示剩余4条评论
3个回答

3

可空类型不能作为属性进行序列化。

您需要更改 Contract 类,以便在 XML 属性中不使用 Nullable,或者将这些属性作为 XML 元素编写到 XML 中。

请尝试以下方法:

public class Contract { 
  [XmlAttribute(AttributeName = "ContractID")] 
  public int ContractID { get; set; } 

  [XmlAttribute(AttributeName= "PostingID")] 
  public string PostingID { get; set; } 

  public System.Nullable<EntryTypeOptions> EntryType { get; set; } 
}

或者:

public class Contract { 
  public int? ContractID { get; set; } 

  [XmlAttribute(AttributeName= "PostingID")] 
  public string PostingID { get; set; } 

  public System.Nullable<EntryTypeOptions> EntryType { get; set; } 
}

0

由于根节点是<Contracts>,请尝试将您的类重新排列为:

[XmlRoot("Contracts")]
public class ContractPosting {
    [XmlElement("Contract", typeof(Contract))]
    public List<Contract> Contracts { get; set; }
}

当您使用XmlArrayXmlArrayItem时,它们必须嵌套在某个东西中。但是,您当前的XmlArray标记实际上是XML文件的根节点,因此它需要是一个XmlRoot
演示:http://ideone.com/jBSwGx

1
ContractIDNullable<int>更改为常规的int,或将其转换为元素而不是属性,然后它应该可以工作-请参见https://dev59.com/lnI95IYBdhLWcg3w-C9b。 - mellamokb

0

谢谢,问题出在可空类型上,我是这样解决的

[XmlIgnore]
public System.Nullable<int> ContractID { get; set; }


[XmlAttribute("ContractID")]
public int ContractIDxml {
get { return ContractID ?? 00; }
set { ContractID = value; }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接