当类继承列表(对象)时序列化 [英] Serializing when class inherits list (of object)

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

问题描述

我有一个 BOOKING 类,它继承了一个(消息)列表我想知道如何序列化这个.我的 BOOKING 类包含 Message 属性和称为合作伙伴、交易和版本的 3 个属性,

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 类有许多属性来创建预订,

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)

使用下面的save as xmlfunction代码

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

知道我哪里出错了吗?

我的BOOKING类如下

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

还有上面代码创建的xml.

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

这是我的反序列化代码

 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

推荐答案

您的类结构混淆了序列化程序.如果 Booking 继承自 List(Of Message) 那么它也有一个 Message 类型的属性 Message(因为它 已经是一种消息类型).它成为一个自我参考.我还做了一些其他问题和调整,并且序列化得很好:

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 在 VB 中不建议为属性赋予与类型相同的名称.因此我使用了 XMessage

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) 您没有正确使用 XMLAttribute.应该有几个IDE错误,XML序列化不需要

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

c) 为了每个数据包处理多个消息,我将 XMsgs 更改为 List 属性.这允许多个消息使用一个 BOOKING 数据集.如果您想要每次预订 1 条消息,则不需要 2 个班级,只需将其设为 List(Of Booking).

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) 摆脱了用作支持字段的私有财产.

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

测试和序列化代码:

   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

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

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