XmlSerializer + Polymorphism

让人想犯罪 __ 提交于 2019-11-29 11:47:01

Because you are using [XmlElement] on the collection property, the corresponding xml is going to be something like:

<GetUserResponse>
    <Users>{this is a user}</Users>
    <Users>{this is a user}</Users>
    <SuperUsers>{this is a super user}</SuperUsers>
    <Users>{this is a user}</Users>
    <SuperUsers>{this is a super user}</SuperUsers>
</GetUserResponse>

there isn't really anywhere it can get a better name for a collection property, other than Items. I wonder if it might be better to use:

[XmlArray("Users")]
[XmlArrayItem("User", typeof(User))]
[XmlArrayItem("SuperUser", typeof(SuperUser))]

in order to build:

<GetUserResponse>
    <Users>
        <User>{this is a user}</User>
        <User>{this is a user}</User>
        <SuperUser>{this is a super user}</SuperUser>
        <User>{this is a user}</User>
        <SuperUser>{this is a super user}</SuperUser>
    </Users>
</GetUserResponse>

then you would have a Users property.

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