سؤال

I have a BOOKING class that inherits a list (of Message) I was wondering how to serialize this. my BOOKING class contains the property of Message and 3 attributes called partner, transaction and version,

My Message class has numerous properties to create a booking,

now when I want to serialize I use this

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.OutwardRoute = "PEMROS"
x.SailingDate = "20120107"
z.Message = x
SaveAsXML(z)

with the save as xmlfunction code below

Public Shared Function SaveAsXML(ByRef val As BOOKING)
    Try

        Dim objStreamWriter As New StreamWriter("c:\ftptest\New Booking\" + val.FileName)
        Dim y As New XmlSerializer(val.GetType)
        y.Serialize(objStreamWriter, val)
        objStreamWriter.Close()

        Return True
    Catch ex As Exception
        Throw ex
    End Try
End Function

any idea where I'm going wrong?

my BOOKING class is 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 Message
    Get
        Return MessageProperty
    End Get
    Set(value As Message)
        MessageProperty = value
    End Set
End Property

Also here is the xml created by the above code.

<?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" />

Here is my deserializing code

 Try
        Dim Samples As BOOKING
        Using objStreamReader As New StreamReader(filepath) 'Path where file is
            Dim x As New XmlSerializer(GetType(BOOKING), New XmlRootAttribute("BOOKING"))
            Samples = x.Deserialize(objStreamReader)
        End Using
        Form1.DataGridView1.DataSource = Samples
        Return True
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
هل كانت مفيدة؟

المحلول

Your class structure is confusing the serializer. If Booking inherits from List(Of Message) then it makes little sense for it to also have a Message Property Of Type Message (because it is already a message type). It becomes a self reference. There are a few other issues and tweaks I made and it serialized fine:

Public Class BOOKING
    Public Property partner As String
    Public Property transaction As String
    Public Property version As String
    Public Property XMsgs As New List(Of XMessage)
End Class


Public Class XMessage
    Public Property A As String
    Public Property C As String
    Public Property B As String
End Class

a) Public Property Message As Message it is ill advised in VB to give a the property the same name as the type. Hence I used XMessage

b) you where not using XMLAttribute correctly. There should have been several IDE errors and it is not needed for XML serialization

c) To handle more than one Message per packet, I changed XMsgs to a List property. This allows one BOOKING dataset for many messages. If you want 1 Msg Per Booking there is no need for 2 classes and just make it a List(Of Booking).

d) got rid of the private property used as a backing field.

Test and serialization code:

   Dim B As New BOOKING

    B.partner = "abcdef"
    B.transaction = "12345"
    B.version = "1.00.1"
    Dim m As New XMessage

    m.A = "foo"
    m.B = "bar"
    B.XMsgs.Add(m)        ' use List prop as a List

    ' Using block to dispose of streamwriter
    Using sr As New System.IO.StreamWriter("c:\Temp\Booking.xml")
        Dim y As New Serialization.XmlSerializer(B.GetType)
        y.Serialize(sr, B)

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