2014-12-01 107 views
2

我在網上看到很多示例,但是我無法理解應用程序,或者示例與我的轉換太不相同。我有一個XML將XML字符串反序列化爲對象VB.NET

<Interfaces> 
    <Interface> 
     <InterfaceCode>987</InterfaceCode> 
     <AccessID>asdf</AccessID> 
     <Password>654321</Password> 
    </Interface> 
    <Interface> 
     <InterfaceCode>789</InterfaceCode> 
     <AccessID>  </AccessID> 
     <Password>  </Password> 
    </Interface> 
</Interfaces> 

而下面的類

<Serializable(), XmlRoot("Interfaces"), XmlType("Interfaces")> 
Public Class InterfacesModel 
    Property Interfaces As New List(Of InterfaceModel) 
End Class 

<Serializable(), XmlType("Interface")> 
Public Class InterfaceModel 
    Property InterfaceCode As String 
    Property AccessID As String 
    Property Password As String 
End Class 

下面的代碼生成一個InterfacesModel一個空Interfaces列表:

Dim str As String = xmlString 
Dim interfaces As InterfacesModel 

Dim serializer As New XmlSerializer(GetType(InterfacesModel)) 
Using reader As TextReader = New StringReader(str) 
    interfaces = serializer.Deserialize(reader) 
End Using 

我希望它來填充接口作爲列出(InterfaceModel的),以便我可以在Interfaces上爲每個接口執行一個操作,併爲每個接口執行一些操作。

回答

6

您的物業上需要XmlElement("Interface")。此外,您可以擺脫XmlType屬性。我不認爲他們正在爲你做任何事情。

<Serializable(), XmlRoot("Interfaces")> 
Public Class InterfacesModel 
    <XmlElement("Interface")> Property Interfaces As New List(Of InterfaceModel) 
End Class 

<Serializable()> 
Public Class InterfaceModel 
    Property InterfaceCode As String 
    Property AccessID As String 
    Property Password As String 
End Class