[英]vb.net xml de-serialization returns empty list
我試圖反序列化一個看起來像這樣的簡單xml
<categories>
<category name="camera">
<tags>
<tag name="6" value="stdy_cam"/>
<tag name="7" value="shaky"/>
</tags>
</category>
<category name="classify">
<tags></tags>
</category>
</categories>
這是我的加載方法 -
Private Sub loadAllCategories()
Dim serializer As XmlSerializer
Using stream As New System.IO.FileStream("C:\temp\tags2.xml", IO.FileMode.Open)
serializer = New XmlSerializer(GetType(List(Of Category)), New XmlRootAttribute("categories"))
Me.Categories = DirectCast(serializer.Deserialize(stream), List(Of Category))
End Using
End Sub
這些是我的課 -
<Serializable()>
Public Class Category
<XmlAttribute("name")>
Public Property Name As String
<XmlArrayItem("tag")>
Public Property Tags As New List(Of Tag)
End Class
<Serializable()>
Public Class Tag
<XmlAttribute("name")>
Public Property Name As String
<XmlAttribute("value")>
Public Property Value As String
End Class
一切似乎都沒問題,但是當我運行程序時,類別列表總是空的。 該程序不會拋出任何錯誤或異常。 當我調試時,它只顯示0作為類別的計數。 我可能做錯了什么?
您在列表中得到零,因為您的類名為Category
而您的元素是category
,小寫c 。 試試這個:
<XmlType(TypeName:="category")>
Public Class Category
<XmlAttribute("name")>
Public Property Name As String
<XmlArrayItem("tag")>
Public Property Tags As New List(Of Tag)
End Class
您也可以刪除serializable屬性。
編輯修復了標簽:
<XmlType(TypeName:="category")>
Public Class Category
<XmlAttribute("name")>
Public Property Name As String
<XmlArray("tags")>
Public Property Tags As New List(Of Tag)
End Class
<XmlType(TypeName:="tag")>
Public Class Tag
<XmlAttribute("name")>
Public Property Name As String
<XmlAttribute("value")>
Public Property Value As String
End Class
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.