简体   繁体   English

DataContractSerializer未序列化一个属性

[英]DataContractSerializer not serializing one property

When I serialize the following class, the ContentPageId XML element is missing from the resulting XML file. 当我序列化以下类时,结果XML文件中缺少ContentPageId XML元素。

[CollectionDataContract(ItemName = "Widget")]
public sealed class StructurePage : List<Widget>, IEquatable<StructurePage>
{
    [DataMember]
    public int ContentPageId
    {
        get;
        set;
    }

    public StructurePage(){}

    public StructurePage(int pageId)
    {
        this.ContentPageId = pageId;
    }

    public bool Equals(StructurePage other)
    {
        return this.ContentPageId.Equals(other.ContentPageId);
    }
}
  1. Why is the property skipped when serializing and how to include it as XML element? 为什么序列化时会跳过该属性,以及如何将其作为XML元素包含?
  2. Is it possible to include it in serialization as an XML attribute to the StructurePage element? 是否可以将其作为StructurePage元素的XML属性包含在序列化中? Was looking for this around the net but could find any info on it, apparently with XmlSerializer there was XmlAttributeAttribute attribute but no such thing with DataContractSerializer. 正在网上寻找此信息,但可以找到有关它的任何信息,显然XmlSerializer具有XmlAttributeAttribute属性,而DataContractSerializer没有此类属性。

Go through this post http://social.msdn.microsoft.com/Forums/eu/wcf/thread/57eb195a-43a9-47fe-8b1a-a9d23feb2df2 浏览这篇文章http://social.msdn.microsoft.com/Forums/eu/wcf/thread/57eb195a-43a9-47fe-8b1a-a9d23feb2df2

According to this 根据这个

Collection data contract classes cannot contain extra data members. 收集数据合同类不能包含额外的数据成员。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM