Multiple XmlElement attributes on same property/class/

风流意气都作罢 提交于 2019-12-13 19:29:55

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/488102/multiple-xmlelement-attributes-on-same-property-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!