且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XML 反序列化仅适用于 xml 中的命名空间

更新时间:2023-11-24 20:40:16

ServiceStack 使用 .NET 的 Xml DataContractSerializer 来序列化 XML 以删除命名空间,您需要将命名空间设置为空字符串:

ServiceStack uses .NET's Xml DataContractSerializer to serialize XML to remove Namespaces you need to either set the Namespace to an empty string with:

[DataContract(Namespace="")]
public class test { ... }

但是,您必须使用 [DataMember] 属性标记要序列化的每个属性.更好的选择是通过在 Assembly.cs 文件中添加和 Assembly 属性来为 C# 命名空间下的所有类型指定一个空命名空间,例如:

But then you'll have to mark each property you want serialized with [DataMember] attributes. A better option is to specify an empty namespace for all types under a C# namespace by adding and Assembly attribute in your Assembly.cs file, e.g:

[assembly: ContractNamespace("", ClrNamespace = "MyServiceModel.DtoTypes")]

注意:您可以删除 [Serializable] 属性 - ServiceStack 的任何序列化程序都不会使用它.此外,像 [XmlRoot] 这样的所有 XmlSerializer 属性都是无用的,因为 ServiceStack 使用 .NET 的 DataContractSerializer 而不是 Microsoft 早期的 XmlSerializer.

Note: you can remove the [Serializable] attribute - it's not used by any of ServiceStack's serializers. Also all XmlSerializer attributes like [XmlRoot] are useless since ServiceStack uses .NET's DataContractSerializer not Microsoft's earlier XmlSerializer.