好吧,因为我无法发表评论,并且在原始问题上寻求帮助,我发现了这个问题,我正在为它做一个新问题(原文:How do I get the value from a specific node in an XML file?)
这是我的两个班级;一个是我的XML文件结构,第二个是列表:
Public Class Member
Public Naam As String
Public Kills As Integer
Public Deaths As Integer
Public KD As Integer
End Class
Public Class Tabel
<XmlElement("Member")> _
Public Members As List(Of Member)
End Class
这是执行它的代码:
Dim serializer As XmlSerializer = New XmlSerializer(GetType(Tabel))
Dim tabel As Tabel = CType(serializer.Deserialize(File.OpenRead("C:\members.xml")), Tabel)
For Each member As Member In tabel
Dim kills As Integer = member.Kills
Next
它应该循环遍历xml文件并按原样执行,但它会在&#34; tabel&#34;上出错。 For Each循环中的最后一个变量。说:表达式是&#39; VBTest.tabel&#39;,它不是集合类型。
提前致谢!
答案 0 :(得分:1)
您需要将Members
属性声明为New
对象,否则会出错。如果你是用于保存集合的外部xml元素,则省略<XmlElement("Member")>
。
Public Class Tabel
'<XmlElement("Member")>
Public Property Members As New List(Of Member)
End Class
输出:
<?xml version="1.0" encoding="utf-8"?>
<tebel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Members>
<Member>
<Id>1</Id>
</Member>
</Members>
如果你添加它们,那么你需要在循环之外的变量。
Dim kills As Integer
For Each member As Member In tabel.Members
kills += member.Kills
Next