我想为下面的C#
创建一个可序列化的XML
类。
<xml>
<versions>
<org.geotools.util.Version>
<version>1.0.0</version>
</org.geotools.util.Version>
<org.geotools.util.Version>
<version>1.1.0</version>
</org.geotools.util.Version>
<org.geotools.util.Version>
<version>2.0.0</version>
</org.geotools.util.Version>
</versions>
<gml>
<entry>
<version>V_20</version>
<gml>
<srsNameStyle>URN2</srsNameStyle>
<overrideGMLAttributes>false</overrideGMLAttributes>
</gml>
</entry>
<entry>
<version>V_10</version>
<gml>
<srsNameStyle>XML</srsNameStyle>
<overrideGMLAttributes>true</overrideGMLAttributes>
</gml>
</entry>
<entry>
<version>V_11</version>
<gml>
<srsNameStyle>URN</srsNameStyle>
<overrideGMLAttributes>false</overrideGMLAttributes>
</gml>
</entry>
</gml>
</xml>
我知道我可以这样做:
[XmlRoot(ElementName = "xml")]
interface IServiceSettings : IXmlSerializable
{
[XmlArray(ElementName = "versions")]
[XmlArrayItem(ElementName = "org.geotools.util.Version")]
IList<string> Versions { get; set; }
}
但它仅适用于仅包含字符串的<org.geotools.util.Version>
元素。如何在我的案例中扩展此代码?
答案 0 :(得分:2)
这个怎么样:
[XmlRoot(ElementName = "xml")]
interface IServiceSettings<T> : IXmlSerializable
{
[XmlArray(ElementName = "versions")]
[XmlArrayItem(ElementName = "org.geotools.util.Version")]
IList<T> Versions { get; set; }
}
然后接口的实现也是通用的
public class ServiceSettings<T> : IServiceSettings<T>