Gavin Stevens's Blog

the ramblings of another developer....

  Home  |   Contact  |   Syndication    |   Login
  34 Posts | 0 Stories | 57 Comments | 212 Trackbacks

News

Archives

Post Categories

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

Feedback

# re: XMLSerializer and Unable to cast type to type 7/31/2007 10:24 PM David
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.

Post A Comment
Title:
Name:
Email:
Comment:
Verification: