Reputation: 21
Hi I have a class BOOKING that inherits list of Message as follows below
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 Message
Get
Return MessageProperty
End Get
Set(value As Message)
MessageProperty = value
End Set
End Property
The Message Class has all the properties to make a booking When I try to serialize nothing is serializing here is the code I am using to set the properties and serialize the booking
Try
Dim z As New BOOKING
Dim x As New Message
z.partner = "company name"
z.transaction = "BOOKING"
z.version = "1.0"
x.MessageType = "C"
x.CustomerNumber = "123"
x.BookingReference = "5845"
x.CustomerBookingReference = "036598"
x.NoDrivers = "1"
z.Message = x
SaveAsXML(z)
Return True
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
and my save as xml is below
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
the xml file results in this
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
The Layout of the booking has to be as follows
<BOOKING>
<Message>
message properties here
</Message>
<BOOKING>
There can only be one message in each booking but for it to be the way it is above I was told it should be in it's own class to get that layout, So I was wondering how to serialize and possibly deserialize the above booking
Upvotes: 0
Views: 125
Reputation: 38875
The BOOKING class listed is invalid: the serializer will be confused by a class which is a List of the same Type as one of its properties. Since there is one Message per Booking packet, you do not need a List at all.
I have no idea where partner, transaction or version
are supposed to show up. As is, I have them as Booking props because thats what your code shows. If there are supposed to be inside the block, then they are actually Message properties and Booking is empty.
Public Class BOOKING
Public Property partner As String
Public Property transaction As String
Public Property version As String
Public Property [Message] As BookingMessage
Public Sub New()
' create a new Msg object
[Message] = New BookingMessage
End Sub
' Message Properties
Public Class BookingMessage
Public Property MessageType As String
Public Property CustomerNumber As String
Public Property BookingReference As String
Public Property CustomerBookingReference As String
Public Property NoDrivers As String
End Class
End Class
Test code:
Dim B As New BOOKING
With B
.partner = "Foo"
.transaction = "ABC"
.Message.BookingReference = "123456"
.Message.CustomerBookingReference = "ziggy"
.Message.NoDrivers = "1"
End With
Dim x As New Xml.Serialization.XmlSerializer(GetType(BOOKING))
x.Serialize(New System.IO.FileStream("C:\Temp\Booking2.xml",
IO.FileMode.OpenOrCreate), B)
Output:
<?xml version="1.0"?>
<BOOKING xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<partner>Foo</partner>
<transaction>ABC</transaction>
<Message>
<BookingReference>123456</BookingReference>
<CustomerBookingReference>ziggy</CustomerBookingReference>
<NoDrivers>1</NoDrivers>
</Message>
</BOOKING>
Upvotes: 1