开发者

Enum value not serializing correctly when converting a Generic.List(Of CustomClass) to XML

开发者 https://www.devze.com 2023-01-10 07:06 出处:网络
I have a Generic.List(Of ImportedVehicle) - ImportedVehicle being a simple class as below. There is an enum property which is marked as public.

I have a Generic.List(Of ImportedVehicle) - ImportedVehicle being a simple class as below.

There is an enum property which is marked as public.

When I serialize to XML using an XMLSerializer, the enum's value is just set to it's default value (which is NotAllocated) and doesn't actually represent the value that is set in code.

Any ideas why an enum's value would not serialize 开发者_高级运维correctly?

 <Serializable()> _
Public Class ImportedVehicle
        Public Property Driver_Name As String = ""
        Public Property Driver_AssignmentType As DriverAllocationTypeEnum = DriverAllocationTypeEnum.NotAllocated
    End Class

    <Serializable()> _
    Public Enum DriverAllocationTypeEnum
        NotAllocated = 1
        NamedDriver = 2
        PoolVehicle = 3
    End Enum

XML output (regardless of the value set to Driver_AssignmentType):

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfImportedVehicle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ImportedVehicle>
    <Driver_Name>Test Name</Driver_Name>
    <Driver_AssignmentType>NotAllocated</Driver_AssignmentType>
  </ImportedVehicle>
  </<ArrayOfImportedVehicle>


Answered my own question. I was being dumb. The value of the enum member was being reset accidently elsewhere in my code before the object was serialized. So it was nothing to do with the serialization process!

0

精彩评论

暂无评论...
验证码 换一张
取 消