2015-06-24 88 views
0

我從來沒有這樣做過,我正在打磚牆。Deserilise XML文檔

這也必須在vb.net中完成,這不是我的事情。

這是XML文件:

<?xml version="1.0" encoding="utf-8" ?> 
<Departures> 
    <Departure> 
    <Destination>Birmingham</Destination> 
    <Service>109</Service> 
    <AimedDepartureTime>13:50</AimedDepartureTime> 
    <ExpectedDepartureTime>13:52</ExpectedDepartureTime> 
    </Departure> 
    <Departure> 
    <Destination>Nuneaton</Destination> 
    <Service>19</Service> 
    <AimedDepartureTime>13:55</AimedDepartureTime> 
    <ExpectedDepartureTime>13:56</ExpectedDepartureTime> 
    </Departure> 
</Departures> 

我需要desrilise並填充它DataGrid控件。 DataGrid的東西,我可以做的。

這是xml文件的解析,我遇到了問題。 我創建了一個類對象來處理XML的佈局/格式。 這是類:

Imports System.Xml.Serialization 

<Serializable()> 
Public Class Departure 
    <System.Xml.Serialization.XmlElement("Destination")> 
    Private DestinationValue As String 
    Public Property Destination() As String 
     Get 
      Return DestinationValue 
     End Get 
     Set(ByVal value As String) 
      DestinationValue = value 
     End Set 
    End Property 
    <System.Xml.Serialization.XmlElement("Service")> 
    Private ServiceValue As String 
    Public Property Service() As String 
     Get 
      Return ServiceValue 
     End Get 
     Set(ByVal value As String) 
      ServiceValue = value 
     End Set 
    End Property 
    <System.Xml.Serialization.XmlElement("AimedDepartureTime")> 
    Private AimedDepartureTimeValue As String 
    Public Property AimedDepartureTime() As String 
     Get 
      Return AimedDepartureTimeValue 
     End Get 
     Set(ByVal value As String) 
      AimedDepartureTimeValue = value 
     End Set 
    End Property 
    <System.Xml.Serialization.XmlElement("xpectedDepartureTime")> 
    Private ExpectedDepartureTimeValue As String 
    Public Property ExpectedDepartureTime() As String 
     Get 
      Return ExpectedDepartureTimeValue 
     End Get 
     Set(ByVal value As String) 
      ExpectedDepartureTimeValue = value 
     End Set 
    End Property 
End Class 


<Serializable()> 
<System.Xml.Serialization.XmlRoot("Departures")> 
Public Class Departures 
    <XmlArray("Departures"), XmlArrayItem("Departure")> 
    Public Departure As Departure() 
End Class 

這是我的調用代碼:

Private Sub btnXmlloader_Click(sender As Object, e As EventArgs) Handles btnXmlloader.Click 
    Try 
     Dim p As New Departures 
     Dim x As New XmlSerializer(p.GetType) 

     'Deserialize text file to a new object. 
     Dim objStreamReader As New StreamReader(Application.StartupPath + "\departures.xml") 
     Dim departures As New Departures() 
     departures = x.Deserialize(objStreamReader) 
     objStreamReader.Close() 


    Catch ex As Exception 

    End Try 
End Sub 

我設置一個斷點,這條線:

departures = x.Deserialize(objStreamReader) 

告訴我,離港什麼。

我在想什麼?

回答

1

更改您的出發類的樣子:

<System.Xml.Serialization.XmlRoot("Departures")> 
Public Class Departures 
    Inherits List(Of Departure) 
End Class 

會看你現在正在試圖像

<!-- one "Departures" for the Departures class --> 
<Departures> 
    <!-- one "Departures" for the Departure property--> 
    <Departures> 
    <Departure ... /> 
    </Departures> 
</Departures> 

另外:

  • Serialization屬性是沒用的XmlSerializer ,這只是將類標記爲可序列化的二進制文件
  • XmlSerializer只能序列化公有屬性,私有字段上的XmlElement屬性將被忽略 - 因此您可以刪除這些屬性;默認情況下屬性被序列化爲名稱等於屬性名稱的元素
+0

非常感謝您的答覆 - 它的工作 - 並感謝您的額外意見。你是一個拯救生命的人! –