【发布时间】:2020-11-17 22:31:47
【问题描述】:
我正在尝试使用对象列表反序列化 XML 文件。我的代码毫无例外地执行,但它总是返回 0 计数 (mRules.Count)?
mRules 总是返回 count = 0,我不知道为什么?我猜这与我的规则类中的对象列表(List(Of Rule))有关......或者可能更简单地使用“名称”作为属性而不是元素?
<Rules>
<Rule name="TV Offset">
<ProductList>
<Product name="Sony" spawnRatio="1">
<Color r=".7" g=".8" b=".7"/>
</Product>
<Product name="LG" spawnRatio="1">
<Color r=".7" g=".8" b=".7"/>
</Product>
</ProductList>
<RegionList>
<Location name="California South"/>
<Location name="Texas East"/>
<Location name="California North"/>
<Location name="Nevada"/>
</RegionList>
</Rule>
<Rule name="Oven Offset">
<ProductList>
<Product name="GE" spawnRatio="1">
<Color r=".2" g=".2" b=".1"/>
</Product>
<Product name="KitchenAid" spawnRatio="2">
<Color r=".2" g=".2" b=".1"/>
</Product>
</ProductList>
<RegionList>
<Location name="California South"/>
<Location name="Texas East"/>
<Location name="Washington North"/>
<Location name="Oregon"/>
</RegionList>
</Rule>
</Rules>
Imports System.Xml.Serialization
<Serializable(), XmlRoot("Rules"), System.Xml.Serialization.XmlInclude(GetType(Rules))>
Public Class Rules
<XmlElement("Rule")>
Private _rule As List(Of Rule)
Public Property Rule() As List(Of Rule)
Get
Return _rule
End Get
Set(ByVal value As List(Of Rule))
_rule = value
End Set
End Property
End Class
Imports System.Xml.Serialization
<Serializable(), System.Xml.Serialization.XmlInclude(GetType(Rule))>
Public Class Rule
<XmlAttribute("name")>
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
' TODO: ProductList
' TODO: RegionList
End Class
Public Sub Load(ByVal vFile As String)
Dim reader As New Xml.Serialization.XmlSerializer(GetType(Rules))
Dim ruleFile As New StreamReader(vFile)
Dim mRules As Rules
mRules = CType(reader.Deserialize(ruleFile), Rules)
End Sub
【问题讨论】:
标签: deserialization xmlserializer