本文介绍了更改 DataMember 的 XML 命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有两个 DataContract,我使用 DataContractSerializer 将它们序列化为 XML.

I have two DataContracts which I am serializing to XML using a DataContractSerializer.

我为两个不同的 DataContract 指定了不同的命名空间,但是每个 DataContract 中都有一个 DataMember 是相同的 POD 类型.此 POD 位于不同的 c# 命名空间中.

I specify different namespace for the two different DataContracts however, there is a DataMember in each DataContract which is of the same POD type. This POD is in a different c# namespace.

我想知道是否有办法指定用于此 DataMember 的命名空间,具体取决于它所属的包含类型.

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.

例如:

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; }
    }
}

我正在寻找可以在上面的代码中提供 SomeNamespaceAttribute 功能的东西.

I'm looking for something which would provide the functionality of SomeNamespaceAttribute in the code above.

注意:我不是在寻求关于如何更好地组织我的 DataContract 的建议,因为不幸的是,我正在重构遗留代码并且无法更改 XML 格式.

Note: I'm not looking for suggestions on how better to organise my DataContracts because unfortunately I'm refactoring legacy code and the XML format can't be changed.

推荐答案

DataContractSerializer 没有公开对 XML 生成的细粒度控制,因此此类属性本身并不可用.但是,您可以对共享类进行子类化(假设您可以去掉 sealed 访问修饰符),使用两个不同的 DataContract 属性和不同的命名空间.

DataContractSerializer does not expose as fine grained control over the XML generation, so this sort of attribute is not inherently available. 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.

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; }
    }
}

如果一切都失败了,您可以使用类似于用于更改自动生成前缀的技术来编辑来自服务的原始 XML.这篇 MSDN 博客文章 详细说明整个过程.

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. This MSDN blog post details the overall process.

这篇关于更改 DataMember 的 XML 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:23