Search code examples
c#xmlxmlserializer

Can we mark a Flag enumeration value as obsolete when using XmlSerializer?


I have seen this [question][1] where it refers to [Obsolete("...")] and it sounded like something that I might need to use. But now I don't think I can.

I have this updated flags enumeration:

[Flags]
[Guid("xxxxx")]
[ComVisible(true)]
public enum AssignmentType
{
    None = 0,
    Attendant = 1,
    ConductorCBS = 2,
    ReaderCBS = 4,
    Chairman = 8,
    Mike = 16,
    PlatformAttendant = 32,
    Prayer = 64,
    OCLM = 128,
    Sound = 256,
    Student = 512,
    Custom = 1024,
    Demonstration = 2048,
    Assistant = 4096,
    Host = 8192,
    CoHost = 16384,
    OCLMTreasures = 32768,
    OCLMGems = 65536,
    OCLMLiving = 131072
}

To give you some context, these flag enumerations appear in my XML file like this:

      <Assignments Attendant="false" ConductorCBS="false" ReaderCBS="false" Chairman="false" Microphones="false" PlatformAttendant="false" Prayer="false" OCLM="false" Sound="false" Student="false" Assistant="false" Host="false" CoHost="false" OCLMTreasures="false" OCLMGems="true" OCLMLiving="false" Demonstrations="false">

Fairly simple stuff. Now, I added an upgrade method to my class:

    public bool UpgradePublisherDatabase()
    {
        bool bSaveDB = false;

        try
        {
            // Database should already be open

            if(PublisherData.Version < 1)
            {
                // CODE SNIPPED

                bSaveDB = true;
            }
            
            if(PublisherData.Version < 2)
            {
                // We need to upgrade the database
                PublisherData.Version = 2;

                var vPublisherKeys = new List<string>(PublisherData.PublisherDictionary.Keys);
                foreach (string strPublisherKey in vPublisherKeys)
                {
                    Publisher oPublisher = PublisherData.PublisherDictionary[strPublisherKey];
                    if (oPublisher.CanUseFor(AssignmentType.OCLM))
                    {
                        // We must set the other OCLM flags
                        oPublisher.SetCanUseFor(true, AssignmentType.OCLMTreasures);
                        oPublisher.SetCanUseFor(true, AssignmentType.OCLMGems);
                        oPublisher.SetCanUseFor(true, AssignmentType.OCLMLiving);
                    }

                    PublisherData.PublisherDictionary[strPublisherKey] = oPublisher;
                }

                bSaveDB = true;
            }

            if(bSaveDB)
            {
                return SavePublisherData();
            }
        }
        catch (Exception ex)
        {
            SimpleLog.Log(ex);
            return false;
        }

        return true;
    }

In my main application (Visual C++ MFC) it calls my DLL to load the XML file. It then runs the Upgrade method on the file to get it up to date.

By this time, when the XML file is at version 2, I no longer require the AssignmentType.OCLM flag in the XML file as I no longer use it. But it does not seem possible to mark just a flag enumerated value as obsolete when serializing?


Update

This is the definition for that attribute:

   [XmlAttribute("OCLM")]
    public bool UseForOCLMAssignments
    {
        get => _Assignments.HasFlag(AssignmentType.OCLM); set
        {
            if (value)
                _Assignments |= AssignmentType.OCLM;
            else
                _Assignments &= ~AssignmentType.OCLM;
        }
    }

So, I want to be able to read it in, but I don't want to write it out. So is it acceptable to use the [Obsolete...] syntax here? [1]: Obsolete attribute causes property to be ignored by XmlSerialization


Solution

  • In the end, this was all I needed:

    
        [Obsolete("Use the OCLMTreasures, OCLMGems and OCLMLiving flag values instead.")]
        [XmlAttribute("OCLM")]
        public bool UseForOCLMAssignments
        {
            get => _Assignments.HasFlag(AssignmentType.OCLM); set
            {
                if (value)
                    _Assignments |= AssignmentType.OCLM;
                else
                    _Assignments &= ~AssignmentType.OCLM;
            }
        }
    
    

    So the Obsolete keyword was the way to go afterall.