反序列化文件

时间:2014-05-08 09:24:27

标签: xml vb.net serialization

您好我有一个BOOKING课程如下:

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

一个消息类如下:

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

我可以使用下面的代码创建和序列化文件。

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

我尝试使用此代码进行反序列化:

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

预订的所有属性都进入datagridview, 但是消息属性应该去哪里我只是在其中有ftpsample.bookingmessage的标题消息,

我的XML文件如下。

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

任何有关如何反序列化的帮助都将非常感谢。

1 个答案:

答案 0 :(得分:1)

它基本上只是序列化代码的反向,但首先你应该在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

通常,如果某些实现了Dispose Property,请使用它,最简单的方法是使用Using块。要反序列化:

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

如果您使用Option Strict(并且您确实应该这样做),Ctype只是将序列化器的返回值从Object转换为Booking。

最后,BookingMessage属性可以自动实现Booking中的属性。在这种情况下,不需要所有代码。