我有50个类标记为DataContractAttribute
。DataContractSerializer无法在名称空间更改后反序列化
这些类形成一个巨大的层次树,使用DataContractSerializer
对xml进行序列化/反序列化。
所有这些指定一个自定义数据合同命名空间[DataContract(Namespace="http://example.com")]
,除了我错过了3个类。
// Old class definitions
[DataContract(IsReference=true)] // <-- forgot ns
public class Type1{}
[DataContract(IsReference=true)] // <-- forgot ns
public class Type2{}
[DataContract(IsReference=true)] // <-- forgot ns
public class Type3{}
[DataContract(IsReference=true, Namespace="http://example.com")] // <-- 47 more like this
public class Type4{}
我希望这三个类可以使用与其他47个类相同的datacontract命名空间。
更改后,我以前保存的所有xml都无法加载。
// Changed to:
[DataContract(IsReference=true, Namespace="http://example.com")] // <-- changed ns
public class Type1{}
[DataContract(IsReference=true, Namespace="http://example.com")] // <-- changed ns
public class Type2{}
[DataContract(IsReference=true, Namespace="http://example.com")] // <-- changed ns
public class Type3{}
[DataContract(IsReference=true, Namespace="http://example.com")]
public class Type4{}
这个方法我试过:
DataContractSerializer - change namespace and deserialize file bound to old namespace
但得到了SerializationException
说Deserialized object with reference id 'i5' not found in stream.
我如何反序列化个XML命名空间变化后和以前保存?
简单的解决方案,工作得很好。 – jayars