同一属性/类/上的多个 XmlElement 属性
我将多个旧版 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为对你来说没有简单的方法。
序列化将会失败,因为一个属性可能有两个不同的值。
哪一个是正确的?
也许我的一些想法可以帮助您...
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.