2009-11-25 93 views
1

我沒有看到我做錯了什麼。爲了看看正在做什麼,我在將XML讀入(我驗證了XML是什麼)後,將構造函數值更改爲「TEST」,類值仍然保留爲「TEST」。更多想法?我已經在另一個課上做這個過程,這個過程很好,無論是我還是一些同事都可以找到差異。XML反序列化不設置類值

XML:

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

類:(試過沒有的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 
+2

無法立即看到問題。您提供的XML看起來好像不是由此代碼生成的 - 將它與通過序列化此類所生成的XML進行比較會有幫助嗎? – Nestor 2009-11-26 00:47:18

+0

不是一個壞主意,不知道爲什麼我沒有想到這一點。讓我見識一下,看看它做了什麼。 – RiddlerDev 2009-11-26 01:02:27

+0

這與問題無關,但您不需要System.Xml.Serialization的''屬性 – MarkJ 2009-11-26 09:36:26

回答

1

好吧,用@maxc的建議,我嘗試了課程的序列化,除了帽子以外沒有什麼不同將我的XML屬性與我的屬性相區分。我不認爲這個東西是區分大小寫的?但是當我改變它以使它們的案例匹配時,它可以工作,甚至可以來回更改以確認。

奇怪嗎?

+0

Xml不區分大小寫。 – CaffGeek 2010-06-28 20:50:24

+0

是的XML是,但VB.Net不是,希望是。 VB.Net庫的老化是「是或者否」。 – RiddlerDev 2010-07-09 16:32:19