Using the Obsolete attribute in .NET causes the XML serializer to ignore the value causing error "validation error: 'Value' is not a valid value for EnumName."

When using .NET 3.5 (rather than .NET 2.0) if you mark a property or enum value as [Obsolete] the property is value is no longer serialized.

For enum values this is a real problem as this prevents the XML from deserializing this value.

/// <summary>
/// The swap file is stored in the virtual machine directory. This value is obsolete and used for backwards compatibility.
/// </summary>
[Obsolete]
vmDirectory = 3,

There is an error in XML document (1, 657). ---> System.InvalidOperationException: Instance validation error: 'Value' is not a valid value for EnumName.

There's no good solution for this as this is a hardcoded change built into .NET 3.5 unless you implement your own serialization code.

The workaround we use is to remove the Obsolete attribute and instead rename the enumeration value with the Obsolete prefix. This will obviously cause the serialization to fail so you can use "XmlEnum" attribute to determine the value to be read.




/// <summary>
/// The swap file is stored in the virtual machine directory. This value is obsolete and used for backwards compatibility.
/// </summary>
[XmlEnum("vmDirectory")]
ObsoleteVmDirectory = 3,



As an additional step you can use the property accessor to "correct" the values when they are accessed.



/// <summary>
/// Gets or sets the location of swap files for the Virtual machines within this cluster.
/// </summary>
[DataComparable(Ignore = true)]
public VMwareClusterSwapFileLocation SwapFileLocation
{
    get
    {
        if (_SwapFileLocation == VMwareClusterSwapFileLocation.ObsoleteHostLocal) { _SwapFileLocation = VMwareClusterSwapFileLocation.HostLocal; }
        if (_SwapFileLocation == VMwareClusterSwapFileLocation.ObsoleteVmDirectory) { _SwapFileLocation = VMwareClusterSwapFileLocation.VmDirectory; }
        return _SwapFileLocation;
    }
    set { _SwapFileLocation = value; }
}
private VMwareClusterSwapFileLocation _SwapFileLocation =  VMwareClusterSwapFileLocation.Unknown;






Comments

Popular posts from this blog

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Enable function lock for F1-F12 on HP ZBook mobile workstations