473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

bug in XmlSerializer.FromTypes()?

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;
}
}
}
Nov 12 '05 #1
1 3075
Sorry for not making myself clear :-) The problem is not inner class,
instead, it's about the "duplicated" classnames even if the classes are in
different scope. In the code sample I gave, the issue is that both T and X
have an inner class called U. In fact, if I rename any of the two Us, the
code will work happily.
"Xing Zhou" wrote:
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;
}
}
}

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
1
by: Bluetears76 | last post by:
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended) I want to serialize the objects and when i am deserizaling i dont know if i am getting...
3
by: Anthony Bouch | last post by:
Hi I've been reading using the XmlSerializer with custom collections. I've discovered that when serializing a custom collection (a class that implements ICollection, IList etc.) the...
4
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
3
by: Loui Mercieca | last post by:
Hi, I have created a class, named FormField , which basically contains two fields, name and value. I have set the tag before the class and the field is set as an XmlAttribute whil the name as...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
4
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is...
0
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After...
3
by: kimtherkelsen | last post by:
Hi, I want to send XML data from a server to some clients over a network connection using the TCP/IP protocol. If I send the XMLs as byte arrays I need to insert header information in the data to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.