问题描述:

I have an XML document that looks something like this:

Both sound and music are derived types from audio base class. They are stored in List sounds collection. How can I deserialize that? Because doing simple (List)x.Deserialize would produce an list of base types.

<sounds>

<sound name="SFX_PunchObject">

<file name="SFX_PunchObject.ogg" size="5414"/>

</sound>

<music name="HWAS_Cutscene1">

<file name="HWAS_Cutscene1.m4a" size="135049"/>

</music>

</sounds>

Thanks!

网友答案:

You can write a custom serializer to deseralize from pretty much any format that contains the necessary information to populate your object..

网友答案:

Normally you can use

[XmlInclude(typeof(ChildType))]

on your base class, and can then deserialize multiple types in one list into the base type. I believe that requires having a

type="music"

attribute, instead of having different root elements, though.

You might have to implement ISerializable to get what you need.

相关阅读:
Top