[英]Change XML Namespace of DataMember
I have two DataContract
s which I am serializing to XML using a DataContractSerializer
. 我有两个DataContract
,我正在使用DataContractSerializer
将其序列化为XML。
I specify different namespace for the two different DataContract
s however, there is a DataMember
in each DataContract
which is of the same POD type. 我为两个不同的DataContract
指定了不同的名称空间,但是,每个DataContract
中都有一个具有相同POD类型的DataMember
。 This POD is in a different c# namespace. 此POD在不同的c#名称空间中。
I would like to know if there is a way of specifying the namespace to use for this DataMember
depending on which containing type it belongs to. 我想知道是否有一种方法可以指定此DataMember
使用的命名空间,具体取决于它属于哪个包含类型。
For example: 例如:
namespace NamespaceShared
{
using System.Runtime.Serialization;
[DataContract]
public sealed class SharedType
{
[DataMember(IsRequired = true)]
public int ValueOne { get; set; }
[DataMember(IsRequired = true)]
public int ValueTwo { get; set; }
}
}
namespace NamespaceOne
{
using System.Runtime.Serialization;
using NamespaceShared;
[DataContract(Namespace = "http://namespace.one")]
public sealed class DataContractOne
{
[DataMember(IsRequired = true)]
//[SomeNamespaceAttribute("http://namespace.one")]
private SharedType SharedValue { get; set; }
}
}
namespace NamespaceTwo
{
using System.Runtime.Serialization;
using NamespaceShared;
[DataContract(Namespace = "http://namespace.two")]
public sealed class DataContractTwo
{
[DataMember(IsRequired = true)]
//[SomeNamespaceAttribute("http://namespace.two")]
private SharedType SharedValue { get; set; }
}
}
I'm looking for something which would provide the functionality of SomeNamespaceAttribute
in the code above. 我正在寻找可以在上面的代码中提供SomeNamespaceAttribute
功能的SomeNamespaceAttribute
。
Note: I'm not looking for suggestions on how better to organise my DataContract
s because unfortunately I'm refactoring legacy code and the XML format can't be changed. 注意:我并不是在寻找有关如何更好地组织DataContract
的建议,因为不幸的是,我正在重构遗留代码,并且XML格式无法更改。
DataContractSerializer
does not expose as fine grained control over the XML generation, so this sort of attribute is not inherently available. DataContractSerializer
并未公开显示对XML生成的细粒度控制,因此这种属性并不是固有可用的。 However, you could subclass the shared class (assuming you can get rid of the sealed
access modifier), with two different DataContract
attributes with different namespaces. 但是,您可以使用两个具有不同名称空间的不同DataContract
属性来子类化共享类(假设您可以摆脱sealed
访问修饰符)。
namespace NamespaceShared
{
using System.Runtime.Serialization;
public class SharedType
{
[DataMember(IsRequired = true)]
public int ValueOne { get; set; }
[DataMember(IsRequired = true)]
public int ValueTwo { get; set; }
}
[DataContract(Namespace = "http://namespace.one")]
public class SharedTypeOne : SharedType
{
}
[DataContract(Namespace = "http://namespace.two")]
public class SharedTypeTwo : SharedType
{
}
}
namespace NamespaceOne
{
using System.Runtime.Serialization;
using NamespaceShared;
[DataContract(Namespace = "http://namespace.one")]
public sealed class DataContractOne
{
[DataMember(IsRequired = true)]
private SharedTypeOne SharedValue { get; set; }
}
}
namespace NamespaceTwo
{
using System.Runtime.Serialization;
using NamespaceShared;
[DataContract(Namespace = "http://namespace.two")]
public sealed class DataContractTwo
{
[DataMember(IsRequired = true)]
private SharedTypeTwo SharedValue { get; set; }
}
}
If all else fails, you could edit the raw XML coming out of the service using a technique similar to that which is used to change the auto-generated prefixes. 如果所有其他方法均失败,则可以使用类似于更改自动生成的前缀的技术来编辑从服务中发出的原始XML。 This MSDN blog post details the overall process. 该MSDN博客文章详细介绍了整个过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.