XML反序列化不设置类值

时间:2009-11-25 21:42:53

标签: xml vb.net .net-3.5 xml-deserialization

我没有看到我做错了什么。为了看看正在做什么,我将构造函数值更改为“TEST”,在读入XML之后(我验证了XML是什么),类值仍然停留在“TEST”。还有什么想法?我已经在另一个工作正常的工作中,我和一些同事都找不到差别。

XML:

<IntervalTranslatorScrubberSetting>
  <LINEINDICATOR_USAGE>USG</LINEINDICATOR_USAGE>
  <FILETYPE>867</FILETYPE>
  <ESIDUNS>8417397824</ESIDUNS>
</IntervalTranslatorScrubberSetting>

CLASS :(在没有XMLElement的情况下尝试过,尝试使用XMLAttribute只看到,没有变化)

Imports System.Xml.Serialization

Namespace Workers.Scrubber
    <Serializable()> _
    Public Class IntervalTranslatorScrubberSetting

#Region "Private Variables"
        Private _ESIDuns As String
        Private _FileType As String
        Private _LineIndicator_Usage As String
#End Region

#Region "Constructors"
        Sub New()
            Me.ESIDuns = "TEST"
            Me.FileType = "TEST"
            Me.LineIndicator_Usage = "TEST"
        End Sub
#End Region

#Region "Serialization"
        Private _SerializMe As New XML(Of IntervalTranslatorScrubberSetting)

        Public Function Serialize(ByVal XMLObject As IntervalTranslatorScrubberSetting) As String
            Return _SerializMe.Serialize(XMLObject)
        End Function

        Public Function Deserialize(ByVal XML As String) As IntervalTranslatorScrubberSetting
            Return _SerializMe.Deserialize(XML)
        End Function
#End Region

#Region "Properties"
        <XmlElement()> _
        Public Property ESIDuns() As String
            Get
                Return _ESIDuns
            End Get
            Set(ByVal value As String)
                _ESIDuns = value
            End Set
        End Property

        <XmlElement()> _
        Public Property FileType() As String
            Get
                Return _FileType
            End Get
            Set(ByVal value As String)
                _FileType = value
            End Set
        End Property

        <XmlElement()> _
        Public Property LineIndicator_Usage() As String
            Get
                Return _LineIndicator_Usage
            End Get
            Set(ByVal value As String)
                _LineIndicator_Usage = value
            End Set
        End Property
#End Region
    End Class
End Namespace

XML处理类:

Imports System.IO
Imports System.Xml

Public Class XML(Of T)
    Private _serializer As New System.Xml.Serialization.XmlSerializer(GetType(T))

    Public Function Serialize(ByVal myobject As T) As String
        'serialise to a memory stream, then read into a string
        Try
            Dim result As String = Nothing
            If myobject IsNot Nothing Then
                Using ms As New MemoryStream
                    Using xtw As New XmlTextWriter(ms, System.Text.Encoding.UTF8)
                        xtw.Formatting = Formatting.Indented
                        _serializer.Serialize(xtw, myobject)
                        'rewind
                        ms.Seek(0, System.IO.SeekOrigin.Begin)
                        Using reader As New StreamReader(ms, Text.Encoding.UTF8)
                            result = reader.ReadToEnd()
                            xtw.Close()
                            reader.Close()
                        End Using
                    End Using
                End Using
            End If
            Return result

        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function Deserialize(ByVal xml As String) As T
        Try
            'default to no object
            If Not String.IsNullOrEmpty(xml) Then
                Using sr As New StringReader(xml)
                    Return CType(_serializer.Deserialize(sr), T)
                End Using
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw
        End Try
    End Function
End Class

1 个答案:

答案 0 :(得分:1)

好的,使用@maxc的建议,我尝试了类的序列化,除了我的XML属性与我的属性的大写之外,没有看到任何不同。我不认为这些东西是区分大小写的?但是当我改变它以使它们的情况匹配时,它可以工作,甚至来回改变以确认。

古怪?