我正在尝试从WebInvoke POST调用中使用XML。反映XML结构和XML本身的主类如下:
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:
[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有些陌生,我觉得我要么非常接近,要么在没有完全理解收藏和合同的情况下自己进入角落,需要采用不同的方法。
任何想法都会有所帮助,谢谢!
答案 0 :(得分:0)
在显示的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;
}