CollectionDataContract 无法识别 DataContract

CollectionDataContract not recognizing DataContract

我正在尝试从 WebInvoke POST 调用中使用 XML。反映XML结构和XML本身的主要class如下:

XML:

<GraphicArea>
  <AnimationID>String content</AnimationID>
  <AutoRetract>true</AutoRetract>
  <ClientID>2147483647</ClientID>
  <Description>String content</Description>
  <GraphicDetails>
    <GraphicDetail xmlns="http://schemas.datacontract.org/2004/07/fogREST">
      <GraphicID>String content</GraphicID>
      <PropFileDescription>String content</PropFileDescription>
      <PropFileID>String content</PropFileID>
      <PropName>String content</PropName>
      <PropValue>String content</PropValue>
    </GraphicDetail>
    <GraphicDetail xmlns="http://schemas.datacontract.org/2004/07/fogREST">
      <GraphicID>String content</GraphicID>
      <PropFileDescription>String content</PropFileDescription>
      <PropFileID>String content</PropFileID>
      <PropName>String content</PropName>
      <PropValue>String content</PropValue>
    </GraphicDetail>
  </GraphicDetails>
  <GraphicSubTypeID>String content</GraphicSubTypeID>
  <GraphicTypeID>String content</GraphicTypeID>
  <GraphicTypeTemplateID>String content</GraphicTypeTemplateID>
  <OffsetX>2147483647</OffsetX>
  <OffsetY>2147483647</OffsetY>
  <OffsetZ>2147483647</OffsetZ>
  <TimeCodeIn>String content</TimeCodeIn>
  <TimeCodeOut>String content</TimeCodeOut>
  <UserID>2147483647</UserID>
</GraphicArea>

数据合同:

[DataContract(Name = "GraphicArea", Namespace = "")]
public class GraphicArea
{
    [DataMember(Name = "ClientID")]
    public virtual int ClientID
    {
        get;
        set;
    }
    [DataMember(Name = "AnimationID")]
    public virtual string AnimationID
    {
        get;
        set;
    }
    [DataMember(Name = "GraphicTypeID")]
    public virtual string GraphicTypeID
    {
        get;
        set;
    }
    [DataMember(Name = "GraphicSubTypeID")]
    public virtual string GraphicSubTypeID
    {
        get;
        set;
    }
    [DataMember(Name = "GraphicTypeTemplateID")]
    public virtual string GraphicTypeTemplateID
    {
        get;
        set;
    }
    [DataMember(Name = "TimeCodeIn")]
    public virtual string TimeCodeIn
    {
        get;
        set;
    }
    [DataMember(Name = "TimeCodeOut")]
    public virtual string TimeCodeOut
    {
        get;
        set;
    }
    [DataMember(Name = "AutoRetract")]
    public virtual bool AutoRetract
    {
        get;
        set;
    }
    [DataMember(Name = "Description")]
    public virtual string Description
    {
        get;
        set;
    }
    [DataMember(Name = "UserID")]
    public virtual int UserID
    {
        get;
        set;
    }

    [DataMember(Name = "GraphicDetails")]
    public GraphicDetailsCollection GraphicDetails
    {
        get;
        set;
    }

    [DataMember(Name = "OffsetX")]
    public virtual int OffsetX
    {
        get;
        set;
    }
    [DataMember(Name = "OffsetY")]
    public virtual int OffsetY
    {
        get;
        set;
    }
    [DataMember(Name = "OffsetZ")]
    public virtual int OffsetZ
    {
        get;
        set;
    }
}

如您所见,我有一个名为 GraphicDetailsCollection 的 CollectionDataContract,其结构如下所示:

[CollectionDataContract]
public class GraphicDetailsCollection : List<GraphicDetail>
{

}

集合本身非常简单,引用了DataContract:

[DataContract]
public class GraphicDetail
{
    [DataMember(IsRequired = false)]
    public string GraphicID;

    [DataMember(IsRequired = false)]
    public string PropName;

    [DataMember(IsRequired = false)]
    public string PropValue;

    [DataMember(IsRequired = false)]
    public string PropFileID;

    [DataMember(IsRequired = false)]
    public string PropFileDescription;
}

我有这个设置是因为 GraphicDetails 中可能有任意数量的 GraphicDetail 部分。我可以处理 XML 范围内的所有数据,但 GraphicDetails 中的 GraphicDetail 内容除外。我遇到的问题是,当我引用 contract.GraphicDetails.Count 以遍历各种 GraphicDetail 集时,我看到 contract.GraphicDetails.Count=0 并且所有这些都是 DataMembers=Null,这是不正确的。

有人可以解释为什么会这样吗?我对 DataContracts 有点陌生,我觉得我要么非常接近,要么在没有完全理解 Collections 和 Contracts 的情况下陷入困境,需要一种不同的方法。

任何想法都会有所帮助,谢谢!

在显示的 XML 中,<GraphicDetail> 有一个 default namespace xmlns="http://schemas.datacontract.org/2004/07/fogREST"。因此,该元素及其所有子元素都在该名称空间中,只要它们缺少明确的名称空间前缀(它们确实如此)。您的数据合同需要反映这一事实:

[CollectionDataContract(Namespace = "http://schemas.datacontract.org/2004/07/fogREST")] // All GraphicDetail child XML elements are to be in the indicated namespace
public class GraphicDetailsCollection : List<GraphicDetail>
{
}

[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/fogREST")] // All data member child XML elements are to be in the indicated namespace
public class GraphicDetail
{
    [DataMember(IsRequired = false)]
    public string GraphicID;

    [DataMember(IsRequired = false)]
    public string PropName;

    [DataMember(IsRequired = false)]
    public string PropValue;

    [DataMember(IsRequired = false)]
    public string PropFileID;

    [DataMember(IsRequired = false)]
    public string PropFileDescription;
}