Deserilise XML Document

49 views Asked by At

I have never done this before and I am hitting a brick wall.

This also has to be done in vb.net which is not my thing.

This is the XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Departures>
  <Departure>
    <Destination>Birmingham</Destination>
    <Service>109</Service>
    <AimedDepartureTime>13:50</AimedDepartureTime>
    <ExpectedDepartureTime>13:52</ExpectedDepartureTime>
  </Departure>
  <Departure>
    <Destination>Nuneaton</Destination>
    <Service>19</Service>
    <AimedDepartureTime>13:55</AimedDepartureTime>
    <ExpectedDepartureTime>13:56</ExpectedDepartureTime>
  </Departure>
</Departures>

I need to desrilise it and populate a datagrid control with it. The DataGrid stuff I can do.

It is the deserilisation of the xml fileI am having issues with. I created a class object to deal with th layout/format of the xml. This is that class:

Imports System.Xml.Serialization

<Serializable()>
Public Class Departure
    <System.Xml.Serialization.XmlElement("Destination")>
    Private DestinationValue As String
    Public Property Destination() As String
        Get
            Return DestinationValue
        End Get
        Set(ByVal value As String)
            DestinationValue = value
        End Set
    End Property
    <System.Xml.Serialization.XmlElement("Service")>
    Private ServiceValue As String
    Public Property Service() As String
        Get
            Return ServiceValue
        End Get
        Set(ByVal value As String)
            ServiceValue = value
        End Set
    End Property
    <System.Xml.Serialization.XmlElement("AimedDepartureTime")>
    Private AimedDepartureTimeValue As String
    Public Property AimedDepartureTime() As String
        Get
            Return AimedDepartureTimeValue
        End Get
        Set(ByVal value As String)
            AimedDepartureTimeValue = value
        End Set
    End Property
    <System.Xml.Serialization.XmlElement("xpectedDepartureTime")>
    Private ExpectedDepartureTimeValue As String
    Public Property ExpectedDepartureTime() As String
        Get
            Return ExpectedDepartureTimeValue
        End Get
        Set(ByVal value As String)
            ExpectedDepartureTimeValue = value
        End Set
    End Property
End Class


<Serializable()>
<System.Xml.Serialization.XmlRoot("Departures")>
Public Class Departures
    <XmlArray("Departures"), XmlArrayItem("Departure")>
    Public Departure As Departure()
End Class

And this is my calling code:

Private Sub btnXmlloader_Click(sender As Object, e As EventArgs) Handles btnXmlloader.Click
    Try
        Dim p As New Departures
        Dim x As New XmlSerializer(p.GetType)

        'Deserialize text file to a new object.
        Dim objStreamReader As New StreamReader(Application.StartupPath + "\departures.xml")
        Dim departures As New Departures()
        departures = x.Deserialize(objStreamReader)
        objStreamReader.Close()


    Catch ex As Exception

    End Try
End Sub

I set a breakpoint and this line:

  departures = x.Deserialize(objStreamReader)

tells me that departures is nothing.

What am i missing?

1

There are 1 answers

1
Ondrej Svejdar On BEST ANSWER

Change your departure class to look like:

<System.Xml.Serialization.XmlRoot("Departures")>
Public Class Departures
    Inherits List(Of Departure)
End Class

What you're trying now will look like

<!-- one "Departures" for the Departures class -->
<Departures>
  <!-- one "Departures" for the Departure property-->
  <Departures>
    <Departure ... />
  </Departures>
</Departures>

Also:

  • Serialization attribute is useless for the XmlSerializer, that just mark class as binary serializable
  • XmlSerializer can serialize only public properites, your XmlElement attributes on private fields are ignored - hence you can drop those; by default property is serialized as element with name equal to property name