XmlSerializer and a Collection<t> Property

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

I am attempting to serialize an object using the XmlSerializer, nothing fancy
pretty standard stuff. The object beign serialized has a Collection<t> typed
property as shown below:

public Collection<QueueInstance> ManagedQueues
{
get
{
return this.queuesManaged;
}
}

QueueInstance is a serializable class with only a few string typed
properties defined. However, I am recieving the following error when I
attempt to serialize the parent object (MsmqManager):

Top Level Exception: "There was an error generating the XML document."

Inner Exception: "The type initializer for
'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMsmqManager' threw an exception."

Inner Inner Exception: "Object reference not set to an instance of an
object."

The collection is initialized in the constructor of the parent object. The
exception si thrown no matter if there is objects in teh collection or not.
I have found some info online about issues with the XmlSerializer and
generics but nothing that really made any since. I have also attempt to
decorate the property with XmlArray and XmlArrayObject attributes but to no
success. Ideas?
 
Michael said:
I am attempting to serialize an object using the XmlSerializer, nothing
fancy
pretty standard stuff. The object beign serialized has a Collection<t>
typed
property as shown below:

public Collection<QueueInstance> ManagedQueues
{
get
{
return this.queuesManaged;
}
}

QueueInstance is a serializable class with only a few string typed
properties defined. However, I am recieving the following error when I
attempt to serialize the parent object (MsmqManager):

Top Level Exception: "There was an error generating the XML document."

Inner Exception: "The type initializer for
'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMsmqManager'
threw an exception."

Inner Inner Exception: "Object reference not set to an instance of an
object."

You need to find out where that NullReferenceException is thrown. It may
have nothing to do with the collection. What happens if you comment out the
collection property?

John
 
Thanks John for the reply. I have been able to do a bit more testing and
have found some interesting things. The issue appears to not be with the
object nor the XmlSerializer (At least as the cause). A bit more information
is needed I think...

I am loading a number Assemblies at startup by the followiing method:

Assembly myAssembly = Assembly.Load(File.ReadAllBytes(assemblyFileLocation));

This works fine all the way up to the point where I serialize one of the
obejcts retrieved from the loaded Assemblies. Now if I use the biding
context of:

Assembly myAssembly = Assembly.LoadFrom(assemblyFileLocation);

The XmlSerializer works great. So I guess my issue is not with the
XmlSerializer neccessarly but with the difference in the Assembly loading
process. Somethiign is differecnt thus casuing the XmlSerializer to puke
when the Assembly is loaded via the COFF based method versus direct file load.
 
Back
Top