Click here to Skip to main content
16,015,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
XML
<?xml version="1.0" encoding="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
XML
<?xml version="1.0" encoding="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
Posted

How about defining your own XML Schema[^] and use xsd.exe[^] to generate your code?

Regards
Espen Harlinn
 
Share this answer
 
You shouldn't care if something is missing. If you're serializing data into an object, that object should know how to correct erroneous data on its own byy applying default values that don't cause the ob ject to throw an exception.
 
Share this answer
 
Comments
Herovato 12-Jan-11 2:45am    
Yes and No.
This object know how to correct data, but I need to know that data is reset to default values!
E.g. this object read state from file, I need to inform user what some data is not in file, and I use default values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900