列表视图未在vb.net中更新

时间:2014-07-14 12:16:21

标签: xml vb.net listview xpath

我正在尝试使用VB.net将播放列表信息从Web服务显示到列表View对象

我已经设法从服务中获取XML并将其转换为字符串并创建了一个循环以将信息输入到列表视图中。问题是它总是多次输入第一首歌的信息

enter image description here Web服务的结果显示在此处

enter image description here

for循环内部的控制台日志是:

<name>Test Song one</name><artist>The tested</artist><album>To test</album>
<name>Test two</name><artist>the Tested</artist><album>To test</album>
<name>Test 3</name><artist>Tested</artist><album>To test</album>

以下是此方法的代码:

        serviceReturn = service.getList(txtPlaylist.Text)
        MessageBox.Show(serviceReturn)
        Dim doc As New XmlDocument

        Dim result As XmlElement = doc.CreateElement("playlist")
        result.InnerXml = serviceReturn
        Dim lstName As XmlNode = result.SelectSingleNode("/name")

        Dim results As XmlNodeList
        results = result.GetElementsByTagName("song")
        lstSong.Items.Add(lstName.InnerText)
        Dim songNod As XmlNode
        Dim listName As ListViewItem
        For Each songNod In results
            listName = New ListViewItem
            Console.WriteLine(songNod.InnerXml)
            Dim lstSongName As XmlNode = songNod.SelectSingleNode("/song/name")
            Dim lstSongArtist As XmlNode = songNod.SelectSingleNode("/song/artist")
            Dim lstSongAlbum As XmlNode = songNod.SelectSingleNode("/song/album")

            listName = New ListViewItem(lstSongName.InnerText)
            listName.SubItems.Add(lstSongArtist.InnerText)
            listName.SubItems.Add(lstSongAlbum.InnerText)
            detailedView.Items.Add(listName)
            listName = Nothing
            lstSongName = Nothing
            lstSongArtist = Nothing
            lstSongAlbum = Nothing

        Next

有人能指出为什么每次只打印第一首歌吗?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试以这种方式更改XPath表达式

    Dim lstSongName As XmlNode = songNod.SelectSingleNode("./name")
    Dim lstSongArtist As XmlNode = songNod.SelectSingleNode("./artist")
    Dim lstSongAlbum As XmlNode = songNod.SelectSingleNode("./album")

您需要引用当前节点,就像现在一样,您始终引用/playlist/song

中相同的第一个节点