-
August 4th, 2008, 10:55 AM
#1
Xml Serialization case insensitive for enum
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?
-
August 4th, 2008, 12:44 PM
#2
Re: Xml Serialization case insensitive for enum
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
August 4th, 2008, 02:37 PM
#3
Re: Xml Serialization case insensitive for enum
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...
-
August 4th, 2008, 02:41 PM
#4
Re: Xml Serialization case insensitive for enum
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.
-
August 4th, 2008, 03:11 PM
#5
Re: Xml Serialization case insensitive for enum
Originally Posted by BigEd781
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|