在同一属性/班多的XmlElement属性/ [英] Multiple XmlElement attributes on same property/class/

查看:183
本文介绍了在同一属性/班多的XmlElement属性/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把一些旧的网络服务和当前的Web服务到相同的后端。

但我必须保持旧的Web服务,有旧的接口兼容。

所以我的问题:

有没有一种方法我可以设置多个属性,例如,财产?

这样的:

  [的XmlElement(AvailableFrom,...我需要什么...)
[的XmlElement(可用,...我需要什么...)
公众的DateTime AvailableFrom {搞定;组;}

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

 私人的DateTime _availableFrom;    [XmlElement的(AvailableFrom)]
    公众的DateTime AvailableFrom
    {
        得到
        {
            返回_availableFrom;
        }
        组
        {
            _availableFrom =价值;
        }
    }    [的XmlElement(可用)]
    市民可用的DateTime
    {
        得到
        {
            返回_availableFrom;
        }
        组
        {
            _availableFrom =价值;
        }
    }


解决方案

我觉得这是对你没有简单的方法。

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

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

1)创建一个XSLT到当前的XML转换成旧格式和背部。
   在XSLT你能够处理不同的价值的最佳方式。

2)不要使用SerialisationAttributes。写你自己的方法,并处理不同的价值观在那里。

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

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

解决方案

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.

这篇关于在同一属性/班多的XmlElement属性/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆