I changed a public property of a class to protected
public class SlideSet : UserModifiable.Properties, Validation.IValidateable
{
public virtual ICollection<Slide> Slides { get; set; }
to
public class SlideSet : UserModifiable.Properties, Validation.IValidateable
{
[DataMember]
protected virtual ICollection<Slide> Slides { get; set; }
but now DataContractSerializer no longer serializes it to xml. My impression from the docs was that the DataMember
attribute was the only thing necessary for this to work.
This is my serialization implementation
public string ToXml(Object obj) {
// set up DataContractSerializer to use the .NET ProxyDataContractResolver, which helps us to serialize EF objects
var entityType = ObjectContext.GetObjectType(obj.GetType());
var knownTypes = ObjectContext.GetKnownProxyTypes();
var resolver = new ProxyDataContractResolver();
var serializer = new DataContractSerializer(entityType, entityType.Name, string.Empty, knownTypes, 1000, true, true, null, resolver);
// serialize to XML string
var stream = new MemoryStream();
serializer.WriteObject(stream, obj);
stream.Seek(0, SeekOrigin.Begin);
return Encoding.UTF8.GetString(stream.ToArray());
}
Why is my protected property not getting serialized?
If you want your XML properly serialized with the DataContractSerializer
, you'll not only need to use DataMemberAttribute
on each serialized property, but also DataContractAttribute
on the class itself.
The two work hand in hand as described in the documentation: