How to XML serialize a class Type using reflection?



I'm trying to XML serialize a class after getting the class through reflection. Can anyone suggest me the exact method. Below is my code.



Assembly myassembly = Assembly.LoadFile(myassemblypath);
List<Type> types = myassembly .GetTypes().Where(x => x.BaseType == typeof(myType));
foreach (Type item in types)
{
dynamic instance = Activator.CreateInstance(item);
using (var writer = new StringWriter())
{
new XmlSerializer(item.GetType()).Serialize(writer, instance);
string xmlEncodedList = writer.GetStringBuilder().ToString();
Console.WriteLine(xmlEncodedList);
}
}


This code is not working, it gives me exception.


No comments:

Post a Comment