سؤال

Hi I Have a BOOKING class as follows:

Public Class BOOKING ': Inherits List(Of Message)
    'Private Property MessageProperty As Message

    <XmlAttribute>
    Public Property partner As String

    <XmlAttribute>
    Public Property transaction As String

    <XmlAttribute>
    Public Property version As String

    Public Property [Message] As BookingMessage

    Public Sub New()
        ' create a new Msg object
        [Message] = New BookingMessage
    End Sub

And the a message class as follows:

Public Class BookingMessage


Private MessageTypeProperty As String = ""
Private CustomerNumberProperty As String = ""
Private BookingReferenceProperty As String = ""
Private CustomerBookingReferenceProperty As String = ""

Public Property MessageType As String
    Get
        Return MessageTypeProperty
    End Get
    Set(ByVal value As String)
        MessageTypeProperty = value
    End Set
End Property

Public Property CustomerNumber As String
    Get
        Return CustomerNumberProperty
    End Get
    Set(ByVal value As String)
        CustomerNumberProperty = value
    End Set
End Property

'Mandatory If MessageType is Either A or D
Public Property BookingReference As String
    Get
        Return BookingReferenceProperty
    End Get
    Set(ByVal value As String)
        BookingReferenceProperty = value
    End Set
End Property

'Optional
Public Property CustomerBookingReference As String
    Get
        Return CustomerBookingReferenceProperty
    End Get
    Set(ByVal value As String)
        CustomerBookingReferenceProperty = value
    End Set
End Property

I can create and serialize the file using the code below.

Public Shared Function SaveAsXML(ByRef val As BOOKING)
    Try
        Dim y As New System.IO.FileStream("C:\ftptest\New Booking\4854.xml",
               IO.FileMode.OpenOrCreate)

        Dim x As New Xml.Serialization.XmlSerializer(GetType(BOOKING))
        x.Serialize(y, val)

        y.Close()
        Return True
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Function

And I've tried to deserialize using this code:

Try
    Dim Samples As New List(Of BOOKING)
    Dim Files As String() = Directory.GetFiles("c:\ftptest\New Booking")
    For Each fl In Files
        'Deserialize XML file
        Dim objStreamReader As New StreamReader(fl)
        Dim i As New BOOKING
        Dim x As New XmlSerializer(i.GetType)
        i = x.Deserialize(objStreamReader)
        Samples.Add(i)
     Next
     Form1.DataGridView1.DataSource = Samples
     Return True
Catch ex As Exception
    Throw ex
End Try

All the properties from booking go into the datagridview, but where the message properties should go I just have the header message with ftpsample.bookingmessage inside it,

My XML file is as follows.

<?xml version="1.0"?>
<BOOKING xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" partner="company name" transaction="BOOKING" version="1.0">
  <Message>
   <MessageType>C</MessageType>
   <CustomerNumber>123</CustomerNumber>
   <BookingReference>5845</BookingReference>
   <CustomerBookingReference>036598</CustomerBookingReference>
  </Message>
 </BOOKING>

Any help on how to deserialize this would be great thanks.

هل كانت مفيدة؟

المحلول

It is basically just the reverse of the code to serialize, but first you should make a change to the serializing part in SaveAsXML:

Using fs As New System.IO.FileStream("C:\ftptest\New Booking\4854.xml",
       IO.FileMode.OpenOrCreate)

     Dim x As New Xml.Serialization.XmlSerializer(GetType(BOOKING))
     x.Serialize(fs, val)

End Using

Generally, if something implementes a Dispose Property, use it and the easiest way is with a Using block. To deserialize:

Dim Bb As BOOKING

Using fs As New System.IO.FileStream(myFileName, IO.FileMode.Open)
    Dim y As New Xml.Serialization.XmlSerializer(GetType(BOOKING))
    Bb = CType(y.Deserialize(fs), BOOKING)
End Using

If you are using Option Strict (and you really should be), Ctype just converts the return from the serializer from Object to Booking.

Finally, BookingMessage properties could be auto implement properties like those in Booking. No real need for all that code in this case.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top