What are the differences between the XmlSerializer and BinaryFormatter
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
XmlSerializer and BinaryFormatter were both used in older .NET applications, but modern guidance treats them very differently. XmlSerializer remains usable for contract-based XML interchange, while BinaryFormatter is obsolete and unsafe for general deserialization. Understanding the differences is important for legacy maintenance and migration planning.
For modern systems, serializer choice should prioritize safety and contract evolution over legacy convenience.
Serialization Format and Interoperability
XmlSerializer produces readable XML text with explicit element names. That makes payloads easier to inspect and share with non-.NET systems.
BinaryFormatter writes binary payloads tied to internal .NET type metadata. Payloads are compact but opaque and tightly coupled to runtime type shapes.
Security Profile Is the Biggest Difference
The most important modern point is security:
BinaryFormatterdeserialization can be exploited with crafted payloads.- It should not be used for untrusted or semi-trusted data.
- New code should avoid it entirely.
Microsoft has marked BinaryFormatter obsolete for security reasons. For most applications, modern alternatives such as System.Text.Json or DataContractSerializer are safer defaults.
Type Model and Control
XmlSerializer works from public properties and fields and typically expects a parameterless constructor. It encourages explicit data contracts.
BinaryFormatter historically serialized rich object graphs automatically, including private state in many cases. That flexibility reduced ceremony but increased fragility and security risk.
Versioning and Compatibility Behavior
Contract-based serializers usually make version evolution clearer:
- add optional fields with defaults
- maintain backward reading behavior
- document schema changes explicitly
Binary graph snapshots tied to internal class structure are harder to evolve safely. Simple refactors such as renaming members or moving namespaces can break deserialization.
For long-lived systems, explicit contracts are usually easier to maintain.
Performance and Payload Tradeoffs
Text formats can be larger than binary formats, but modern serializers are fast enough for many workloads. If performance is critical, benchmark safe options instead of relying on legacy assumptions.
Example JSON baseline with System.Text.Json:
When size is critical, evaluate protocol buffers or MessagePack in addition to JSON and XML.
Migration Strategy Away from BinaryFormatter
For legacy applications, migrate incrementally:
- inventory all serialization and deserialization entry points
- classify trusted and untrusted data paths
- replace high-risk external deserialization first
- add compatibility readers for historical stored data
- remove
BinaryFormatterusage fully after validation
This phased plan reduces operational risk.
Example of Safe Contract-Oriented Serialization
Even if you still need XML for interoperability, keep contracts explicit.
Add schema validation or business-rule validation after deserialization if data quality is critical.
Testing During Serializer Migration
Migration should include:
- round-trip tests for new serializer
- fixtures with old payloads
- malformed payload rejection tests
- cross-version compatibility tests
These tests catch silent contract drift and help prevent production data corruption.
Common Pitfalls
A common mistake is assuming serializer replacement is purely syntactic. In reality, payload format and compatibility semantics change.
Another mistake is keeping BinaryFormatter in network or file-import paths because migration seems inconvenient.
A third mistake is ignoring versioning policy when switching to contract-driven serializers.
Teams also forget to test historical data migration and discover breakage only after deployment.
Summary
XmlSerializeris text-based and contract-oriented, whileBinaryFormatteris legacy binary and obsolete.- Security concerns make
BinaryFormatterunsuitable for modern deserialization scenarios. - Contract-based serializers are easier to evolve, audit, and interoperate with.
- Benchmark safe modern serializers before making performance decisions.
- Plan migrations in phases with compatibility and regression tests.

