✅ DataContractSerializer with polymorphisme

Hello everyone. I need help with a topic. I would like to serialize this XML file:
<?xml version="1.0" encoding="utf-8" ?>
<DataBase>
<Movie Title="Matrix">
<Duration>110</Duration>
</Movie>
<Movie Title="IronMan">
</Movie>
<Movie Title="Test">
</Movie>
</DataBase>
<?xml version="1.0" encoding="utf-8" ?>
<DataBase>
<Movie Title="Matrix">
<Duration>110</Duration>
</Movie>
<Movie Title="IronMan">
</Movie>
<Movie Title="Test">
</Movie>
</DataBase>
Here's my class that contains my database
[DataContract(Name = "DataBase", Namespace = "")]
public class VideoDataBase
{
[DataMember] // ????
public List<Movie> Movies { get; set; }

[OnDeserializing]
public void OnDeserializing(StreamingContext context)
{
Console.WriteLine("Database on Deserializing");
}

[OnDeserialized]
public void OnDeserialized(StreamingContext context)
{
Console.WriteLine("Database has been deserialized");
}


}
[DataContract(Name = "DataBase", Namespace = "")]
public class VideoDataBase
{
[DataMember] // ????
public List<Movie> Movies { get; set; }

[OnDeserializing]
public void OnDeserializing(StreamingContext context)
{
Console.WriteLine("Database on Deserializing");
}

[OnDeserialized]
public void OnDeserialized(StreamingContext context)
{
Console.WriteLine("Database has been deserialized");
}


}
and my generic class
[DataContract]
[KnownType(typeof(Movie))]
public abstract class VideoDatabaseItem
{
[DataMember]
public string Title { get; set; }

[OnDeserializing]
public void OnDeserializing(StreamingContext context)
{
Console.WriteLine("VideoDatabaseItem on Deserializing");
}

}
[DataContract]
[KnownType(typeof(Movie))]
public abstract class VideoDatabaseItem
{
[DataMember]
public string Title { get; set; }

[OnDeserializing]
public void OnDeserializing(StreamingContext context)
{
Console.WriteLine("VideoDatabaseItem on Deserializing");
}

}
and finally my Movie Class, which is a derived class of VideoDatabaseItem.
[DataContract]
public class Movie : VideoDatabaseItem
{
[DataMember(Name = "Duration")]
public string Duration { get; set; }

[DataMember(Name = "Title")]
public string Title { get; set; }

[OnDeserializing]
public new void OnDeserializing(StreamingContext context)
{
Console.WriteLine("Movie on Deserializing");
}
}
[DataContract]
public class Movie : VideoDatabaseItem
{
[DataMember(Name = "Duration")]
public string Duration { get; set; }

[DataMember(Name = "Title")]
public string Title { get; set; }

[OnDeserializing]
public new void OnDeserializing(StreamingContext context)
{
Console.WriteLine("Movie on Deserializing");
}
}
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
Pobiega
Pobiega10mo ago
I'm not familiar with [DataContract] deserialization, must it use that or is the [XmlElement] family of attributes an acceptable replacement?
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
jessica Boisserand
jessica BoisserandOP10mo ago
Can I use XmlElement in a WPF project with polymorphic objects? I got the impression that DataContracts were more appropriate, am I mistaken ?
Pobiega
Pobiega10mo ago
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
jessica Boisserand
jessica BoisserandOP10mo ago
ahah is just a basic test 🙂
Pobiega
Pobiega10mo ago
Well, if you actually want a file-based database for your WPF app, might I suggest SQLite? 🙂
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega10mo ago
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
jessica Boisserand
jessica BoisserandOP10mo ago
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 😬
Pobiega
Pobiega10mo ago
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
public class Movie : DbEntry
{
[XmlAttribute]
public string Title { get; set; }
[XmlElement]
public int? Duration { get; set; }
}

public class Database
{
[XmlElement(nameof(Movie), typeof(Movie))]
[XmlElement(nameof(Comic), typeof(Comic))]
public List<DbEntry> Elements { get; set; }
}

public class Comic : DbEntry
{
[XmlAttribute]
public string Text { get; set; }
}

public abstract class DbEntry
{
}
public class Movie : DbEntry
{
[XmlAttribute]
public string Title { get; set; }
[XmlElement]
public int? Duration { get; set; }
}

public class Database
{
[XmlElement(nameof(Movie), typeof(Movie))]
[XmlElement(nameof(Comic), typeof(Comic))]
public List<DbEntry> Elements { get; set; }
}

public class Comic : DbEntry
{
[XmlAttribute]
public string Text { get; set; }
}

public abstract class DbEntry
{
}
that works fine for this xml:
<?xml version="1.0" encoding="utf-8" ?>
<Database>
<Movie Title="Matrix">
<Duration>110</Duration>
</Movie>
<Movie Title="IronMan">
</Movie>
<Comic Text="Yes">
BodyContent
</Comic>
</Database>
<?xml version="1.0" encoding="utf-8" ?>
<Database>
<Movie Title="Matrix">
<Duration>110</Duration>
</Movie>
<Movie Title="IronMan">
</Movie>
<Comic Text="Yes">
BodyContent
</Comic>
</Database>
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 😄
jessica Boisserand
jessica BoisserandOP10mo ago
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
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server