It seems that XmlSerializer.FromTypes() method cannot handle inner class
properly .. or is there anything I did wrong?
The following code will throw a reflection exception at FromTypes() method.
Is there a work-around?
using System;
using System.Xml;
using System.Xml.Serialization;
namespace My
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
Console.WriteLine("-- This is OK.---");
XmlSerializer s1 = new XmlSerializer(typeof(T));
XmlSerializer s2 = new XmlSerializer(typeof(X));
Console.WriteLine("--- This will throw exception! ---");
XmlSerializer[] s3 = XmlSerializer.FromTypes(new Type[]
{typeof(T), typeof(X)}); // use My.T result in same problem
}
catch(Exception e)
{
Console.WriteLine("=========================");
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.WriteLine("=========================");
}
Console.Write("Enter any key to continue ..");
Console.In.Read();
}
}
[XmlRootAttribute("T")]
public class T
{
[System.Xml.Serialization.XmlElement("myU", Type = typeof(U))]
public U myU;
[System.Xml.Serialization.XmlRootAttribute("U")]
public class U
{
[System.Xml.Serialization.XmlElement("name")]
public string name;
}
}
[XmlRootAttribute("X")]
public class X
{
[System.Xml.Serialization.XmlElement("myU", Type = typeof(U))]
public U myU;
[System.Xml.Serialization.XmlRootAttribute("U")]
public class U
{
[System.Xml.Serialization.XmlElement("Xname")]
public string name;
}
}
}