Click here to Skip to main content
15,909,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm having a problem serialising a List of Objects.

Each object is a class, whose attributes are derived from a schema table returned by an SQL query.

The data-type of each object is determined using reflection and at the same time a .dll is built.

The eventual plan is that after the data is retrieved, the newly populated List of Objects is serialised to XML and then de-serialised into a separate application. To gain access to the attributes of the newly created POCO a '.dll' is generated.

The problem arises when attempting to serialise this class:

List<Type> noDuplicates = expectedTypes.Distinct().ToList(); // captured when creating the .dll
// start of serialization process
con.Message( "starting serialization" );
XmlSerializer serializer = new XmlSerializer(/* Trouble arises here */ , noDuplicates.ToArray<Type>());
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, outputList); // Throws exception "Unable to generate XML document


When passing the type into the XmlSerializer I am unsure how to get the type of the reflected POCO that has been created.

Thank you in advance for any feedback or solutions.

Larrythemule

-- Addition --

Hi,

I have tried this since posting last time:

XML
const string TypeName = "myType";
//Code
TypeBuilder tb = mb.DefineType( TypeName, TypeAttributes.Public | TypeAttributes.Serializable );
// Some Code
Type generatedType = tb.CreateType();
ab.Save( aName.Name + ".dll" );
XmlSerializer serializer = new XmlSerializer(generatedType, noDuplicates.ToArray<Type>()); 



This was an attempt to pass the correct type into the XMLSerialiser. Instead of throwing an exception immediately, there was a pause, suggesting that the document was at least partially serialised. The same exception was thrown however.

Thanks again.
Posted
Updated 8-Nov-10 4:29am
v3

Hi,

If you don't must to use a List , you can use an Array

XML
Type[] noDuplicates = expectedTypes.Distinct().ToArray(); 
con.Message( "starting serialization" );
XmlSerializer serializer = new XmlSerializer(/* Trouble arises here */ , noDuplicates.ToArray());
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, outputList); // Throws exception "Unable to generate XML document


you can try this

good luck

jerem
 
Share this answer
 
Comments
jim lahey 8-Nov-10 10:32am    
He already does:

XmlSerializer serializer = new XmlSerializer(/* Trouble arises here */ , noDuplicates.ToArray
Laurence1234 8-Nov-10 10:33am    
Hi Jim and Jerem,
Thanks for your feedback Jerem, Jim is right though I'm afraid as I'm already converting my List into an Array of Types.

Type[] myArray [gives the same data structure as] myList.ToArray< Type >()
Hi,

Whilst I think that this technically answers the question I have asked here, the overall problem is not solved.

Type ListOfGeneratedObjType = typeof(List<>).MakeGenericType(new Type[] { generatedType });// Creates a List of my new reflected types
//This new list can then be passed to the XmlSerializer and treated as "List<NewType>"
XmlSerializer serializer = new XmlSerializer(ListOfGeneratedObjType, noDuplicates.ToArray<Type>());



There are still issues with the serialisation process but it seems more likely now that is due to the data that is being passed into the list.

The "XmlSerializer" class should be able to properly serialise from a list with those given parameters, as when stepping into the code I can confirm that the "ListOfGeneratedObjType" is correct.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900