我在从包含特定属性的父节点读取子节点时遇到小问题。
这是我的xml:
<Players>
<Group Sort="Attack">
<Player Name="John"/>
<Player Name="John"/>
</Group>
<Group Sort="Defense">
<Player Name="Thomas"/>
<Player Name="Frank"/>
</Group>
</Players>
这是我的代码:
Dim FullList As New XmlDocument
FullList.Load("FullList.xml")
Dim ReadPlayer as string = Nothing
Dim ReadList As XmlNodeList = FullList.SelectNodes("/Players/Group")
For Each ReadNode As XmlNode In ReadList
If ReadNode IsNot Nothing Then
Dim ReadNodeAttribute as XmlAttribute = ReadNode .Attributes("Sort")
If ReadNodeAttribute IsNot Nothing Then
If ReadNodeAttribute.Value = "Attack" then
Dim answer As String = "YES"
Dim NameList As XmlNodeList = FullList.SelectNodes("/Players/Group[@Sort = '" & ReadNodeAttribute.Value & "' ]/Player")
For Each Name As XmlNode In NameList
If Name IsNot Nothing Then
Dim NameAttribute As XmlAttribute = Name.Attributes("Name")
If NameAttribute IsNot Nothing Then
MsgBox(NameAttribute.Value & answer)
End If
End If
Next
End If
End If
End If
Next
问题是我不明白NameAttribute.Value
我认为选择节点有问题,但我不确定具体在哪里。