I have small issue with deserializing the object of type ReaderAttributes
. As you can see, internally it uses OnOff
class to set some of the fields to be "ON"
or "OFF"
After the object of ReaderAttributes
deserialized, all the fields except OnOff
are correctly set from XML file, rest are just set to false. I ran through the debugger, to see how ReaderAttributes
is being constructed, and noticed that at very end of Sub New()
it runs through the Public Sub New()
of the OnOff
eight times (number of variables of type OnOff
in ReaderAttributes
).
<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
And here is the code to that I have used to deserialize.
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 (Rather long):
"<?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>";
For those of you who might have similar issue. Here the solution. All it took to fix the bug, is to remove constructors from the
OnOff
class, and within the body of the class, and within the body of the class, set the class data members to their default values.