Serializing without XmlInclude Serializing without XmlInclude xml xml

Serializing without XmlInclude


XmlSerializer has a constructor that accepts an array of types that will be accepted when deserializing:

public XmlSerializer(   Type type,   Type[] extraTypes);

You should be able to pass your array of assemblyTypes as the second argument.


You can an array of types into the Xml Serializer as David Norman has shown. One huge word of caution. Every time you do this a new xml serializer is built and compiled. If you do this a lot you will have a huge memory leak and performance hog on your hand.

This is a huge memory and performance hog, make sure you only do this once.You can resolve this by caching your xml Serializer: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

Snippet from MSDN:

Dynamically Generated Assemblies To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified types. The infrastructure finds and reuses those assemblies. This behavior occurs only when using the following constructors:

XmlSerializer..::.XmlSerializer(Type)

XmlSerializer..::.XmlSerializer(Type, String)

If you use any of the other constructors, multiple versions of the same assembly are generated and never unloaded, which results in a memory leak and poor performance. The easiest solution is to use one of the previously mentioned two constructors. Otherwise, you must cache the assemblies in a Hashtable, as shown in the following example.