XML serialization of interface property
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
XmlSerializer in .NET works best when it knows the concrete shape of every serialized object ahead of time. Interface-typed properties break that assumption, because an interface describes behavior but does not tell the serializer which concrete class should be written or reconstructed.
Why Interface Properties Fail
Consider this simple model:
This design is fine for normal C# code, but XmlSerializer cannot directly serialize PaymentMethod. During serializer generation it needs a concrete serializable type, not an interface.
That is why code such as this typically fails:
The problem appears before any XML is even written.
A Practical Workaround: Ignore The Interface, Expose A Concrete XML Property
The most common workaround is to keep the interface for your domain model while exposing a serializer-friendly property for XML:
This pattern keeps the public XML contract concrete while still letting the rest of the code work with the interface.
Supporting Multiple Implementations
If the interface may point to several concrete types, each one must be declared explicitly for the XML property:
Without those explicit mappings, deserialization still has no safe way to know which concrete types are valid.
When A DTO Is Cleaner
If the XML shape is an integration boundary rather than your internal domain model, a dedicated DTO is often cleaner than forcing XML concerns into your core objects.
For example, you can create an XML model like OrderXml that uses concrete types only, then map it to the interface-based domain model after deserialization.
That separation is useful when:
- the domain model is polymorphic
- the XML contract is fixed by another system
- versioning concerns differ between business logic and transport format
In those cases, a DTO can reduce attribute noise and make the serialization rules more explicit.
Common Pitfalls
One common mistake is leaving the interface property public and unignored. The serializer still sees it and fails even if you added a helper property elsewhere.
Another issue is exposing the XML-facing property as object but forgetting to declare the allowed concrete types with XML attributes.
A third problem is trying to make XmlSerializer solve deep polymorphism automatically. It is intentionally more rigid than many JSON serializers.
Finally, teams sometimes overload the domain model with transport-specific attributes until the class becomes harder to maintain than a separate DTO would have been.
Summary
- '
XmlSerializercannot directly serialize an interface property because interfaces are not concrete types.' - The usual fix is to ignore the interface property and expose a concrete XML-facing surrogate.
- If several implementations are allowed, declare each concrete type explicitly.
- For larger or more polymorphic models, separate XML DTOs are often cleaner than forcing XML rules into domain classes.
- Keep the serializer contract concrete even if the internal application code stays interface-based.
Related reading
- XmlNode Value vs InnerText
- XmlSerializer - There was an error reflecting type
- XmlSerializer remove unnecessary xsi and xsd namespaces
- XPath and XSLT 2.0 for .NET?
- xUnit.net Global setup teardown?
- You must add a reference to assembly ''netstandard, Version2.0.0.0
- Your project is not referencing the .NETFramework,Versionv4.5 framework.
- A definitive guide to API-breaking changes in .NET

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.