我有一个非常简单的XML文件,看起来像:
<Reshelving>
<Location>LOC1</Location>
<Location>LOC2</Location>
</Reshelving>
我有一个vb.net表单,我想使用以下代码在列表框中显示位置文本:
Public Function loadFile(ByRef filename As String) As Boolean
Try
reshelvingDocument.Load(filename)
Dim myNodes As XmlNodeList
Dim name As XmlNode
myNodes = reshelvingDocument.SelectNodes("Reshelving")
lstHomeLocations.Items.Clear()
For Each location As XmlNode In myNodes
name = location.SelectSingleNode("Location")
If name Is Nothing Then
Continue For
End If
lstHomeLocations.Items.Add(name.InnerText)
Next
If lstHomeLocations.Items.Count = 0 Then
Return False
End If
lstHomeLocations.SelectedIndex = 0
reshelvingFile = filename
Me.Text = "Reshelving Settings - " & reshelvingFile
Return True
Catch ex As Exception
Dim message As String = ex.Message
Return False
End Try
End Function
Sub LstHomeLocationsSelectedIndexChanged(sender As Object, e As EventArgs)
Dim HomeLocation As XmlNode, HomeLocationData As XMLNode
HomeLocation = reshelvingDocument.SelectSingleNode("Reshelving[Location=""" & lstHomeLocations.SelectedItem & """]")
End Sub
表单只显示第一个节点(LOC1),但不显示任何其他节点。
答案 0 :(得分:0)
我认为你还需要一个循环。 myNodes将包含示例中的一个节点,但会有2个子节点,每个子节点都有内部文本。也许更像是:
For Each location As XmlNode In myNodes
For Each n As XmlNode In location.SelectNodes("Location")
lstHomeLocations.Items.Add(n.InnerText)
Next
Next