1
您好我有一個艙位如下:反序列化文件
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>
任何關於如何反序列化的幫助將非常感謝。
你可以發佈一些編譯代碼嗎?你的班級定義無效。 – DeanOC
現在試試我忘了把它放在私人物業。 – kpjm0501
參考[this](http://msdn.microsoft.com/en-us/library/cca2d5s9%28v=vs.71%29.aspx) –