Hello,
I have some class and wish to store it in XML file and then read it again. So I use XMLSerialiser, and it works fine, but I don't know how to handle situation if someone change XML file.
E.g. my class Save method produse this XML file
="1.0"="utf-8"
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FileName>test.txt</FileName>
<SheetName>List</SheetName>
<FirstRow>0</FirstRow>
</Data>
if someone delete all elements inside and leave only
="1.0"="utf-8"
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</Data>
my code don't get any error or any exception, but leave properties which are missing in XML file uninitialized.
I want to know about such events, how can I achieve this?
Code:
<XmlRootAttribute("Data")> _
Public Class SeriazableClass
#Region "Members"
Private _FileName As String
Private _SheetName As String
Private _FirstRow As Integer
#Region "Properties"
Property FileName() As String
Get
Return _FileName
End Get
Set(ByVal value As String)
_FileName = value
End Set
End Property
Property SheetName() As String
Get
Return _SheetName
End Get
Set(ByVal value As String)
_SheetName = value
End Set
End Property
Property FirstRow() As Integer
Get
Return _FirstRow
End Get
Set(ByVal value As Integer)
_FirstRow = value
End Set
End Property
#End Region
#End Region
#Region "Methods"
Public Sub New()
End Sub
Public Sub Save()
Dim serializer As New XmlSerializer(GetType(SeriazableClass))
Dim writer As New StreamWriter("test.xml")
serializer.Serialize(writer, Me)
writer.Close()
End Sub
Public Shared Function Read() As SeriazableClass
Dim serializer As New XmlSerializer(GetType(SeriazableClass))
Try
Dim reader As New StreamReader("test.xml") 'there is possible file-not-found
Try
Read = serializer.Deserialize(reader) 'there is possible invalid-xml
Finally
reader.Close()
End Try
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & " during reading", , "Error reading xml file")
Read = New SeriazableClass
Finally
End Try
End Function
#End Region
End Class