CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2008
    Posts
    18

    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?

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    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...

  4. #4
    Join Date
    Jun 2008
    Posts
    18

    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.

  5. #5
    Join Date
    Jun 2008
    Posts
    18

    Re: Xml Serialization case insensitive for enum

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured