Click to See Complete Forum and Search --> : Xml Serialization case insensitive for enum


TheIrishThug
August 4th, 2008, 10:55 AM
I have an xml file used to load settings into my program. One of the settings is the logging level. If the user can't get the program to start, I'll often ask them to manually change the level from "Off" to "Debug". The problem is that is the user writes "debug" deserialization fails because the lower case isn't expected. Is there an extra flag I can set on this property to let the Xml reader know that it should convert to the enum value without regard to case?

boudino
August 4th, 2008, 12:44 PM
Isn't it easier to ask customer to write it right? This can be achieved without recompiling and redistributing the application.

Otherwise, I'm affraid that you have not other chance than implement ISerializable.

BigEd781
August 4th, 2008, 02:37 PM
So, I could be wrong here because I have not done this myself, but couldn't you just change the enum value to all lower or all caps and then check the XML value.ToLower() or .ToUpper()? Again, sorry if I am missing something in the de-serialization process that would prohibit this...

TheIrishThug
August 4th, 2008, 02:41 PM
OK, I was just checking to see if something like that existed. Sometimes it's better to stop a user from causing the error rather than just saying user error.

TheIrishThug
August 4th, 2008, 03:11 PM
So, I could be wrong here because I have not done this myself, but couldn't you just change the enum value to all lower or all caps and then check the XML value.ToLower() or .ToUpper()? Again, sorry if I am missing something in the de-serialization process that would prohibit this...

When you use XmlSerializer, it does everything for you. You pass it an XmlReader and it spits back an object that matches one of the objects you've created in your code. Therefore, I'm not really in control when the text to enum conversion is being attempted.

I decided to just catch the exception and transform it into a message to the user.