Search code examples
c#xmlxml-serializationdatacontractserializer

DataContractSerializer not serializing one property


When I serialize the following class, the ContentPageId XML element is missing from the resulting XML file.

[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?
  2. Is it possible to include it in serialization as an XML attribute to the StructurePage element? 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.

Solution

  • Go through this post 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.