Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Deserialization of invalid enum values

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
steinmr@gmail.com - 10 Feb 2008 11:42 GMT
I have a class with an enum field which is serialized. For whatever
reason, the enum value that was serialized, no longer exists in the
enum. When I deserialize, I get an InvalidOperationException. Is there
any way of ignoring this error in the xml document, set the enum to
the default value, and continue deserialization?

Sample program below, throws an exception on the last line, but not if
the line before it is removed...

public class Program
{
    public class MyClass
    {
        public MyEnum MyEnumField = MyEnum.MyEnumValue;
    }
    public enum MyEnum
    {
        MyEnumValue
    }
    static void Main(string[] args)
    {
        MyClass myClass = new MyClass();
        System.Xml.Serialization.XmlSerializer xmlSerializer
            = new System.Xml.Serialization.XmlSerializer(typeof(MyClass));
        System.IO.StringWriter stringStream = new System.IO.StringWriter();
        xmlSerializer.Serialize(new System.Xml.XmlTextWriter(stringStream),
myClass);
        string serializedString = stringStream.ToString();

        // Deserialize after modification
        serializedString = serializedString.Replace("MyEnumValue",
"AnInvalidEnumValue");
        MyClass deserializedClass
            = (MyClass)xmlSerializer.Deserialize(
                new System.Xml.XmlTextReader(new
System.IO.StringReader(serializedString)));
    }
}
Nicholas Paldino [.NET/C# MVP] - 10 Feb 2008 15:08 GMT
The only way I can think of to get around this is to implement
IXmlSerializable and handle the serialization/deserialization yourself,
ignoring the enum value if it doesn't exist anymore.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I have a class with an enum field which is serialized. For whatever
> reason, the enum value that was serialized, no longer exists in the
[quoted text clipped - 34 lines]
> }
> }
Ignacio Machin ( .NET/ C# MVP ) - 10 Feb 2008 18:45 GMT
Hi,

Not that I know of. After all it's an error!

The only workaround I see is to mark the item as not serializable and
instead serialize an integer representation . then after you reconstruct the
object you can decide what to do.
Or even better, instead of having a member of enum type has it of integer
and create a pblic property of type Enum. there in the get you convert it to
int, if the int stored is not longer in the enm you will get an error and
you must handle it accordingly.

>I have a class with an enum field which is serialized. For whatever
> reason, the enum value that was serialized, no longer exists in the
[quoted text clipped - 34 lines]
> }
> }
steinmr@gmail.com - 11 Feb 2008 14:52 GMT
Thank you for your answers. I was hoping there was an easier path than
IXmlSerializeable, as the rest of the class does not need special
treatment.
Serializing as int is an option, but not ideal (I would like to keep
my enums int-free...)

I was hoping for an XmlIgnoreError-attribute or something like that,
but I guess I have to live with it...

On Feb 10, 7:45 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.com> wrote:
> Hi,
>
[quoted text clipped - 50 lines]
> > }
> > }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.