在类继承另一个类的列表时进行序列化 [英] Serializing while class inherits list of another class

查看:44
本文介绍了在类继承另一个类的列表时进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 BOOKING,它继承下面的消息列表

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

Message Class 拥有进行预订的所有属性当我尝试序列化时没有序列化这里是我用来设置属性和序列化预订的代码

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

我的另存为 xml 如下

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

xml 文件导致这个

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

预订的布局必须如下

<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

推荐答案

列出的 BOOKING 类无效:序列化程序将被一个与其属性之一类型相同的 List 类混淆.由于每个 Booking 包有一个 Message,因此您根本不需要 List.

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.

我不知道合作伙伴、交易或版本应该出现在哪里.照原样,我将它们作为 Booking 道具,因为这就是您的代码显示的内容.如果应该在块内,那么它们实际上是 Message 属性,而 Booking 为空.

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

测试代码:

    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)

输出:

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

这篇关于在类继承另一个类的列表时进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆