从XML反序列化的对象具有某些字段的默认值

时间:2014-11-24 19:54:26

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

对于ReaderAttributes类型的对象进行反序列化,我遇到了一个小问题。如您所见,内部使用OnOff类将某些字段设置为"ON""OFF"

ReaderAttributes反序列化的对象之后,除了OnOff之外的所有字段都是从XML文件中正确设置的,其余字段只是设置为false。我浏览了调试器,看看ReaderAttributes是如何构建的,并注意到在Sub New()的最后,它遍历Public Sub New() OnOff八次(数字) OnOffReaderAttributes类型的变量。

<Serializable, XmlRoot("ReaderAttributes")>
Public Class ReaderAttributes
    '  <TTY>OFF</TTY>
    Public tty As OnOff
    '  <ECHO>OFF</ECHO>
    Public echo As OnOff
    '  <BAUD>115200</BAUD>
    Public baud As String
    '  <XONXOFF>OFF</XONXOFF>
    Public xonxoff As OnOff
    '  <FIELDSEP>" "</FIELDSEP>
    Public fieldsep As String
    '  <IDREPORT>OFF</IDREPORT>
    Public idreport As OnOff
    '  <NOTAGRPT>OFF</NOTAGRPT>
    Public notagrpt As OnOff
    '  <CHECKSUM>OFF</CHECKSUM>
    Public checksum As OnOff
    '  <IDTRIES>3</IDTRIES>
    Public idtries As Integer
    '  <RDTRIES>3</RDTRIES>
    Public rdtries As Integer
    '  <WRTRIES>3</WRTRIES>
    Public wrtries As Integer
    '  <SELTRIES>1</SELTRIES>
    Public seltries As Integer
    '  <UNSELTRIES>1</UNSELTRIES>
    Public unseltries As Integer
    '  <LOCKTRIES>1</LOCKTRIES>
    Public locktries As Integer
    '  <INITTRIES>1</INITTRIES>
    Public inittries As Integer
    '  <ANTTRIES>1</ANTTRIES>
    Public anttries As Integer
    '  <BRI>ON</BRI>
    Public bri As OnOff
    '  <ANTS>1,2,0,0,0,0,0,0</ANTS>
    Public ants As String
    '  <TAGTYPE>G1</TAGTYPE>
    Public tagtype As String
    '  <EPCIDREPORT>OFF</EPCIDREPORT>
    Public epcidreport As OnOff
    '  <SWTTADDR>18</SWTTADDR>
    Public swttaddr As Integer
    '  <SWTTLEN>1</SWTTLEN>
    Public swttlen As Integer
    '  <POWER>100</POWER>
    Public fieldstrength As String
    Public Sub New()
        tty = New OnOff()
        echo = New OnOff()
        baud = "115200"
        xonxoff = New OnOff()
        fieldsep = " "

        idreport = New OnOff(True)
        idreport.onoff = True

        notagrpt = New OnOff()
        checksum = New OnOff()
        idtries = 3
        rdtries = 3
        wrtries = 3
        seltries = 1
        unseltries = 1
        locktries = 1
        inittries = 1
        anttries = 1
        bri = New OnOff()
        bri.onoff = True
        ants = "1"
        tagtype = "EPCC1G2"
        epcidreport = New OnOff()
        swttaddr = 12
        swttlen = 6
        fieldstrength = "100,100,100,100"
    End Sub
End Class

Public Class OnOff
    Public onoff As Boolean
    Public Sub New()
        onoff = False
    End Sub
    Public Sub New(ByVal val As Boolean)
        onoff = val
    End Sub
    Public Overrides Function ToString() As String
        If Me.onoff Then
            Return "ON"
        Else
            Return "OFF"
        End If
    End Function
End Class

以下是我用来反序列化的代码。

Dim serializer As New XmlSerializer(typ)
Dim o As Object 'Object to be deserialized 
Using reader As XmlReader = XmlReader.Create(New StringReader(str))
      o = serializer.Deserialize(reader)

XML(相当长):

"<?xml version=\"1.0\"?>\" & vbCrLf & \"<ReaderAttributes xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\" & vbCrLf & \"  <tty>\" & vbCrLf & \"    <_onoff>false</_onoff>\" & vbCrLf & \"  </tty>\" & vbCrLf & \"  <echo>" & vbCrLf & \"    <_onoff>false</_onoff>\" & vbCrLf & \"  </echo>\" & vbCrLf & \"  <baud>115200</baud>\" & vbCrLf & \"  <xonxoff>\" & vbCrLf & \"    <_onoff>false</_onoff>\" & vbCrLf & \"  </xonxoff>\" & vbCrLf & \"  <fieldsep>\" \"</fieldsep>\" & vbCrLf & \"  <idreport>\" & vbCrLf & \"    <_onoff>true</_onoff>\" & vbCrLf & \"  </idreport>\" & vbCrLf & \"  <notagrpt>\" & vbCrLf & \"    <_onoff>false</_onoff>\" & vbCrLf & \"  </notagrpt>\" & vbCrLf & \"  <checksum>\" & vbCrLf & \"    <_onoff>false</_onoff>\" & vbCrLf & \"  </checksum>\" & vbCrLf & \"  <idtries>3</idtries>\" & vbCrLf & \"  <rdtries>3</rdtries>" & vbCrLf & \"  <wrtries>3</wrtries>\" & vbCrLf & \"  <seltries>1</seltries>\" & vbCrLf & \"  <unseltries>1</unseltries>\" & vbCrLf & \"  <locktries>1</locktries>\" & vbCrLf & \"  <inittries>1</inittries>\" & vbCrLf & \"  <anttries>1</anttries>\" & vbCrLf & \"  <bri>" & vbCrLf & \"    <_onoff>true</_onoff>\" & vbCrLf & \"  </bri>\" & vbCrLf & \"  <ants>1,4</ants>\" & vbCrLf & \"  <tagtype>EPCC1G2</tagtype>\" & vbCrLf & \"  <epcidreport>\" & vbCrLf & \"    <_onoff>false</_onoff>\" & vbCrLf & \"  </epcidreport>\" & vbCrLf & \"  <swttaddr>12</swttaddr>\" & vbCrLf & \"  <swttlen>6</swttlen>\" & vbCrLf & \"  <fieldstrength>100,100,100,100</fieldstrength>\" & vbCrLf & \"</ReaderAttributes>";

2 个答案:

答案 0 :(得分:1)

您的Public onoff As Boolean成员变量名与XML中的<_onoff>元素之间存在脱节。您可以重命名变量:

Public _onoff As Boolean

或者使用XmlElement属性告诉序列化程序元素名称:

<XmlElement(ElementName:="_onoff")>
Public onoff As Boolean

无论哪种方式都应该正确地反序列化。

答案 1 :(得分:0)

对于那些可能有类似问题的人。这里的解决方案。 修复bug的方法是从OnOff类中删除构造函数,在类的主体内,在类的主体内,将类数据成员设置为默认值。