I only want only to de-serializing a certain data member, without serializing it.
I understand I can set EmitDefaultValue =false, and set the value to null.
<Which serializer?If this is XmlSerializer
then either:
public int Foo {get;set;}
[XmlIgnore]
public bool FooSpecified {
get { return false; } // never serialize
set { }
}
or
public int Foo {get;set;}
public bool ShouldSerializeFoo() { return false; }
will do this. A quick test shows that this doesn't work for DataContractSerializer
, though. protobuf-net also supports both of these, for info.
Have you tried decorating the property with [IgnoreDataMember]
?
There is the attribute System.Xml.Serialization.XmlIgnoreAttribute wich say to xmkserializers to ignore your property. But it only change xml serialization behavior.
You can change the value of the data member before the serialization (to the default value, so it doesn't get serialized), but then after the serialization you'd change it back - using the [OnSerializing]
and [OnSerialized]
callbacks (more information in this blog post). This works fine as long as you don't have multiple threads serializing the object at the same time.
public class StackOverflow_8010677
{
[DataContract(Name = "Person", Namespace = "")]
public class Person
{
[DataMember]
public string Name;
[DataMember(EmitDefaultValue = false)]
public int Age;
private int ageSaved;
[OnSerializing]
void OnSerializing(StreamingContext context)
{
this.ageSaved = this.Age;
this.Age = default(int); // will not be serialized
}
[OnSerialized]
void OnSerialized(StreamingContext context)
{
this.Age = this.ageSaved;
}
public override string ToString()
{
return string.Format("Person[Name={0},Age={1}]", this.Name, this.Age);
}
}
public static void Test()
{
Person p1 = new Person { Name = "Jane Roe", Age = 23 };
MemoryStream ms = new MemoryStream();
DataContractSerializer dcs = new DataContractSerializer(typeof(Person));
Console.WriteLine("Serializing: {0}", p1);
dcs.WriteObject(ms, p1);
Console.WriteLine(" ==> {0}", Encoding.UTF8.GetString(ms.ToArray()));
Console.WriteLine(" ==> After serialization: {0}", p1);
Console.WriteLine();
Console.WriteLine("Deserializing a XML which contains the Age member");
const string XML = "<Person><Age>33</Age><Name>John Doe</Name></Person>";
Person p2 = (Person)dcs.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(XML)));
Console.WriteLine(" ==> {0}", p2);
}
}
add the IgnoreDataMemberAttribute