同一属性/类/上的多个 XmlElement 属性

发布于 2024-07-12 07:49:56 字数 821 浏览 5 评论 0原文

我将多个旧版 Web 服务和当前的 Web 服务放入同一个后端。

但我必须保持旧的网络服务与旧的界面兼容。

所以我的问题是:

有没有办法可以设置多个属性,例如属性?

像这样:

[XmlElement("AvailableFrom",... what I need...)]
[XmlElement("Available",... what I need...)]
public DateTime AvailableFrom{get; set;}

一种解决方案是创建额外的属性,但我真的不喜欢代码膨胀。

    private DateTime _availableFrom;

    [XmlElement("AvailableFrom")] 
    public DateTime AvailableFrom
    {
        get
        {
            return _availableFrom;
        }
        set
        {
            _availableFrom = value;
        }
    }

    [XmlElement("Available")] 
    public DateTime Available
    {
        get
        {
            return _availableFrom;   
        }
        set
        {
            _availableFrom = value;
        }
    }

I'm putting several legacy web services and the current web service into the same back end.

But I have to keep the old web services compatible with there old interface.

So my question:

Is there a way I can set several attributes on, for example, a property?

Like this:

[XmlElement("AvailableFrom",... what I need...)]
[XmlElement("Available",... what I need...)]
public DateTime AvailableFrom{get; set;}

One solution would be creating extra properties, but I really don't like the code bloat.

    private DateTime _availableFrom;

    [XmlElement("AvailableFrom")] 
    public DateTime AvailableFrom
    {
        get
        {
            return _availableFrom;
        }
        set
        {
            _availableFrom = value;
        }
    }

    [XmlElement("Available")] 
    public DateTime Available
    {
        get
        {
            return _availableFrom;   
        }
        set
        {
            _availableFrom = value;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2024-07-19 07:49:56

我认为对你来说没有简单的方法。

序列化将会失败,因为一个属性可能有两个不同的值。
哪一个是正确的?

也许我的一些想法可以帮助您...

1) 创建一个 XSLT 将当前的 xml 转换为旧格式并返回。
在 XSLT 中,您能够以最佳方式处理不同的值。

2) 不要使用 SerializationAttributes。 为其编写自己的方法并处理其中的不同值。

3) 使用您的类作为基础并创建两个子类。
使用覆盖和序列化属性填充两个子类。

I think there is no simple way for you.

Serialization will fail because there could be two different values for one property.
Which one is than the right one?

Perhaps some of my ideas can help you...

1) Create an XSLT to transform the current xml into the old format and back.
In XSLT you are able to handle different values the best way.

or

2) Do not use SerialisationAttributes. Write your own method for it and handle the different values there.

or

3) Use your class as base and create two child classes.
Fill the two child classes with overrides and the attributes for serialization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文