✅ DataContractSerializer with polymorphisme
Hello everyone. I need help with a topic. I would like to serialize this XML file:
Here's my class that contains my database
and my generic class
and finally my Movie Class, which is a derived class of VideoDatabaseItem.
I'm having trouble serializing my list with the Movie item, for example. I feel like the inheritance isn't working properly.
I absolutely want to have a generic class for my list of items because later on, I want to be able to serialize other objects.
It would be great if one of you could help me with this topic 🙏🏻
12 Replies
I'm not familiar with
[DataContract]
deserialization, must it use that or is the [XmlElement]
family of attributes an acceptable replacement?Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Can I use XmlElement in a WPF project with polymorphic objects? I got the impression that DataContracts were more appropriate, am I mistaken ?
If you are just reading/writing from/to an .XML file on disk, you absolutely can
DataContracts seem to be related to WCF, as far as I can tell
ahah is just a basic test 🙂
Well, if you actually want a file-based database for your WPF app, might I suggest SQLite? 🙂
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
We will help you with polymorphic XML deserialization, if that is the root of your problem here, but if your root problem is actually that you want a file based database, there are much better approaches
Sorry, I made a mistake with WPF, but I meant to refer to WCF here... Unfortunately, the topic is imposed, and I cannot choose SQLite, i know it's quite sad 😢
mhmhmmh okey I've been stuck for a while despite many attempts; I must not be approaching it in the right way 😬
Okay if its WCF you dont have much of an option I think
but for the record, here is how I'd do it with "normal" xml
that works fine for this xml:
As far as I can tell,
DataContractSerializer
doesn't support polymorphism without using a type discriminator
or hm, maybe...
Well, its not possible with that XML structure
DataContracts dont allow "inline arrays", so you must have an explicit list wrapper
Its a fucking mess thou for sure 😄Thank you very much for your help !!! 🙏🏻 I was really stuck! I'm currently testing your solution, but it seems to be what I want to do. 🙃
perfect, it's work for me !! Thannnk youu 🙂
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View