Let's say I have the following classes:
namespace A.B.C
{
[DataContract]
public Data
{
[DataMember] public int Value1 {get;set;}
[DataMember] public double Value2 {get;set;}
[DataMember] public MyClass Value3 {get;set;}
}
}
namespace X.Y.Z
{
[DataContract]
public Dataset
{
[DataMember] public Data Data1 {get;set;}
[DataMember] public int Data2 {get;set;}
// ...
}
}
I haven been serializing and deserializing Dataset
objects with the default DataContractSerializer
. Now, during refactoring I changed the namespace of Data
from A.B.C
to A.B.C.D
and deserialization no longer works: The DataMembers of Data
are all 0.
I tried to fix this by adding the old namespace to the DataContract:
namespace A.B.C.D
{
[DataContract(Namespace = "A.B.C")]
public Data
{
[DataMember] public int Value1 {get;set;}
[DataMember] public double Value2 {get;set;}
[DataMember] public MyClass Value3 {get;set;}
}
}
But that does not fix it. I also tried a clean rebuild and redeploy (Windows Store App), just in case, but still nothing.
How do I fix the deserialization?
If the namespace is not specified explicitly, it is prefixed. What you do need is to decorate the class with something like:
[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/A.B.C")]