XML Serialization vs Reflection in C#


<a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx" rel="nofollow">XML Serialization from MSDN</a>:


Serializes and <strong>deserializes objects</strong> into and from XML documents. The XmlSerializer enables you to control how objects are encoded into XML.


<a href="http://msdn.microsoft.com/en-us/library/ms173183(VS.80).aspx" rel="nofollow">Reflection from MSDN</a>


Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or <strong>get the type from an existing object and invoke its methods or access its fields and properties</strong>. If you are using attributes in your code, Reflection enables you to access them.


As far as my understanding goes, I could create objects in run time using XML Serialization? In other words, let's say I have a database, I could define my "classes" or "objects" in couple of tables. I could then get the XML for the object's data and then create the object at run-time.

I could also already have those objects compiled as libraries readily available and then use Reflection to access it's functions.

From your understanding, which one of these two concepts would grant the most flexibility while sacrificing the least performance? Bonus points if you can provide a detailed explanation with considerations and perhaps a sample of code.


Serialization and Reflection are not mutually exclusive. You could definitely serialize and deserialize an object and then subsequently modify it using Reflection.


Serialization is simply the concept of taking a 'snapshot' of an object's state so that you can potentially restore that snapshot at a later time.

If you wish to store objects in a persistent store, serialization is a good option if you don't need to be able to query after particular values.

Note that there are at least two different types of serialization:

<ul><li><em>XML Serialization</em>, that represents an object as XML. Since it is XML, this representation is (in theory at least) human-readable and interoperable.</li> <li><em>Binary serialization</em>, that simply stores and reads an object as an array of bytes. This representation is proprietary and not human-readable.</li> </ul>


Reflection is the ability to use object metadata to manipulate an object. You could, for example, decide that you want to assign the string "Foo" to all writable string properties of a given object, irrespective of the type of object.

This is mostly interesting when the type of object is not known at design time.


To use deserialize an object you need to know the type you want to deserialize it into. So you can't just create you objects from xml without having its type defined in an assembly.

What you can do is to store xml in your database that represents serialized objects. You could then load the xml from the database and deserialize it into object instances as needed. The source of the xml need not be a serialized object, so you can create it manually if you need.

Using reflection would be a different situation. With reflection you could take an object and get a list of the method and properties available on the object. You could then write code that worked dynamically with your objects regardless of which type they implement. The problem with this approach is that the code is clumsy to write and read, and it is very easy to introduce errors that are only visible at run time. On top of that the code will run slowly due to the overhead introduced by using reflection.

Instead of using reflection I would have my objects implement some well known interfaces that I could cast them to. That would allow my code to be type-safe and I could avoid the hassle of reflection. The code would also run much faster and be more readable.


You cannot create new types on the fly using neither XML serialization nor reflection. These techniques only applies to existing types. If you need to create new types at runtime you will have to use another approach. However, generating types of the fly is of limited usefulness since you can only use reflection to access these types. Using the dynamic runtime in the next major release of .NET will give you more options for creating and using dynamic types.

XML serialization is for serializing objects to and from a well known format (XML). Reflection is much more general and enables you to inspect type information at runtime and manipulate objects without knowing their type at compile time. You can also do serialization using reflection, but it is much more cumbersome compared to XML serialization.


  • JS generate random boolean
  • How do I extract just a specific portion of a code snippet from multiple files, that may be differen
  • PHP cannot connect to LDAP Oracle Directory Server Enterprise Edition
  • Winforms: Browser-like TabControl
  • ANTLR4 Mutual left recursion
  • What is the best way to draw a large number of lines using openGL and cocos2D?
  • How can I properly import the environment from running a subcommand in Perl?
  • does elasticsearch have compound indexes?
  • Stream data into rotating log tables in BigQuery
  • Find generic sub-lists within a list
  • Fixed frame rate using android Camera2 api
  • Why does checkout sometimes stage a file?
  • SEO friendly 301 redirect .htm to .aspx
  • draw a B+ tree in latex
  • iOS - Is this a task for enums?
  • VB.Net Double comparison after some additions
  • oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Invalid JWT
  • Extracting individual digits from a float
  • Mongodb update() vs. findAndModify() performace
  • Zeromq with python hangs if connecting to invalid socket
  • It is possible use the same sql azure instance from two different cloud service of two different sub
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • How to draw a line dynamically in android [duplicate]
  • Salesforce Different WSDL files and when to use
  • Android Studio Can't Find tools.jar
  • TFS 2015 - Waiting for an agent to be requested
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • JqueryMobile Popup menu is not working
  • Reading a file into a multidimensional array
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • Android application: how to use the camera and grab the image bytes?
  • Zurb Foundation _global.scss meta styles for js?
  • Row Count Is Returning the incorrect number using RaptureXML
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • How reduce the height of an mschart by breaking up the y-axis
  • Join two tables and save into third-sql
  • How to make Safari send if-modified-since header?
  • Traverse Array and Display in markup