I have a data class that is serialized with the DataContractSerializer
. The class uses the [DataContract]
attribute with no explicit Namespace
declaration. As such, the namespace in the resulting xml file is generated based on the namespace of the class.
The class basically looks like this:
namespace XYZ
{
[DataContract]
public class Data
{
[DataMember(Order = 1)]
public string Prop1 { get; set; }
[DataMember(Order = 2)]
public int Prop2 { get; set; }
}
}
...and the resulting xml:
<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/XYZ">
<Prop1>StringValue</Prop1>
<Prop2>11</Prop2>
</Data>
Now I want to change the namespace of the class (actually remove it) by changing the [DataContract]
attribute to [DataContract(Namespace = "")]
. However, once I do this any file previously serialized with the original namespace with no longer deserialize. I receive the following exception:
Error in line 1 position XXX. Expecting element 'Data' from namespace ''.. Encountered 'Element' with name 'Data', namespace 'http://schemas.datacontract.org/2004/07/XYZ'.
This makes perfect sense. I changed the namespace. I'm ok with that. However, it seems like there must be a way to tell the DataContractSerializer
to go ahead and deserialize that data even though the namespaces don't match.