© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
hugeman

❔ XML deserialise different types into a single list

I'm using the build in XML serialisation library (unless it's with .NET framework?), and I current have it deserialise a collection of elements into a list of that element type. This is my class:
public class Shortcut {
    [XmlElement("Keystroke")] public List<Keystroke> KeyStrokes { get; set; }
}

public class Keystroke {
    [XmlAttribute("Keycode")] public string KeyCode { get; set; }
    [XmlAttribute("Mods")]    public string Mods { get; set; }
}
public class Shortcut {
    [XmlElement("Keystroke")] public List<Keystroke> KeyStrokes { get; set; }
}

public class Keystroke {
    [XmlAttribute("Keycode")] public string KeyCode { get; set; }
    [XmlAttribute("Mods")]    public string Mods { get; set; }
}

But I also want to add a Mousestroke too (LMB, RMB, etc), but I want to maintain the order that they are placed in the XML document, meaning I need them to go into 1 list right? Is it possible to do that for 2 different types or is that a limitation in the serialiser?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Loading XML results in list longer than XML
C#CC# / help
14mo ago
❔ Is there a pattern for having a single collection of different object types?
C#CC# / help
3y ago
Deserialise part of a json response
C#CC# / help
2y ago