Geeks With Blogs
Gavin Stevens's Blog the ramblings of another developer....

I recently had a strange problem with XMLSerialization throwing the error: There was an error creating the XML Document.  Unable to cast type Configuration to type Configuration

Here was the simple configuration class:

Serializable]public class Configuration

{

public bool Active;

public string lastPSM = "";

 }

and the Save method:

public

{

XmlSerializer serial = new XmlSerializer(typeof(Configuration));

TextWriter writer = new StringWriter();

serial.Serialize(writer, config);

Console.WriteLine(writer.ToString());

writer.Close();

}

cant get much easier than that, but everytime i called Serialize I would get the error, so I changed the code to this:

public

{

XmlSerializer serial = new XmlSerializer(config.GetType());

TextWriter writer = new StringWriter();

serial.Serialize(writer, config);

Console.WriteLine(writer.ToString());

writer.Close();

}

Low and behold that fixed it, and now, funny enough, changing the code back to the way it was before works fine now...  Anyone care to share why things like this would happen?  I even tried closing, re-opening studio to make sure something hadn't gotten whacked...  strange I can't make it break now while before I couldnt make it work...

Ahhh the joys of development....

Posted on Tuesday, July 31, 2007 6:22 PM | Back to top


Comments on this post: XMLSerializer and Unable to cast type to type

# re: XMLSerializer and Unable to cast type to type
Requesting Gravatar...
Hello Gavin,

I had a similar problem which I blogged about here on my other blog: http://keystrokes.davidcalculli.info/2007/05/paragon-programming-update.html

You'll find a nice picture here: http://i176.photobucket.com/albums/w171/auroruster/paragon/invalid_casting_of_same_type.png

Its a crazy error in the .Net Reflection which sometimes is solved by doing something a different way.
Left by David on Jul 31, 2007 10:24 PM

Your comment:
 (will show your gravatar)


Copyright © Gavin Stevens | Powered by: GeeksWithBlogs.net | Join free