Results 1 to 6 of 6

Thread: Help with XML in visual basic.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    16

    Help with XML in visual basic.

    Hey people. I'm making a program that uses Spotify's Web API to search for music, it gives everything in a XML file, that looks like this (a search for a song called ho hey) http://ws.spotify.com/search/1/track?q=Ho+Hey

    The problem I'm having is to get the track names, album info and artist info.
    This is the code I got so far, or the function for searching.
    (some msgboxes are made for testing, so don't mind those)

    Public Function Search(ByVal Songname As String, ByVal mode As Integer)
    TextBox1.Text = Songname
    Dim url As String
    Dim reader As XDocument
    If Songname = ("") Then
    Else
    Try
    If mode = 1 Then
    url = ("http://ws.spotify.com/search/1/track?q=" & (Songname))
    ElseIf mode = 2 Then
    url = ("http://ws.spotify.com/search/1/artist?q=" & (Songname))
    ElseIf mode = 3 Then
    url = ("http://ws.spotify.com/album/1/track?q=" & (Songname))
    End If
    MsgBox(url)
    reader = XDocument.Load(url)
    MsgBox("Done")
    Catch ex As Exception
    Return False
    MsgBox(ex.ToString & (" DSAD"))
    End Try
    End If
    For Each tracks As XElement In reader...<tracks>...<track>
    Dim songnaaame As String = reader.Element("name")
    MsgBox(songnaaame)
    Next

    End Function

  2. #2
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with XML in visual basic.

    What is the problem? where does your code go wrong?

    Looks like mode 1 and 3 are the same

    with that said this might help you a bit

    http://www.codeproject.com/Articles/...sing-in-VB-NET

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Help with XML in visual basic.

    try this:

    Code:
    Public Function Search(ByVal Songname As String, ByVal mode As Integer) As Boolean
        'TextBox1.Text = Songname
        Dim url As String = ""
        Dim reader As XDocument = Nothing
        If Songname = ("") Then
        Else
            Try
                If mode = 1 Then
                    url = ("http://ws.spotify.com/search/1/track?q=" & (Songname))
                ElseIf mode = 2 Then
                    url = ("http://ws.spotify.com/search/1/artist?q=" & (Songname))
                ElseIf mode = 3 Then
                    url = ("http://ws.spotify.com/album/1/track?q=" & (Songname))
                End If
                'MsgBox(url)
                reader = XDocument.Load(url)
                'MsgBox("Done")
            Catch ex As Exception
                Return False
                MsgBox(ex.ToString & (" DSAD"))
            End Try
        End If
    
        Dim ns As XNamespace = XNamespace.Get("http://www.spotify.com/ns/music/1")
    
        'MsgBox(reader.Root.Elements(ns + "track").Count())
    
        For Each track As XElement In reader.Root.Elements(ns + "track")
            Dim name As String = track.Element(ns + "name").Value
            Dim artistNode As XElement = track.Element(ns + "artist")
            Dim artistName As String = artistNode.Element(ns + "name").Value
            Dim albumNode As XElement = track.Element(ns + "album")
            Dim albumName As String = albumNode.Element(ns + "name").Value
            Dim released As String = albumNode.Element(ns + "released").Value
            ListView1.Items.Add(New ListViewItem(New String() {name, artistName, albumName, released}))
        Next
    
    End Function

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Help with XML in visual basic.

    Quote Originally Posted by billboy View Post
    What is the problem? where does your code go wrong?

    Looks like mode 1 and 3 are the same

    with that said this might help you a bit

    http://www.codeproject.com/Articles/...sing-in-VB-NET
    they're not:

    Code:
    If mode = 1 Then
        url = ("http://ws.spotify.com/search/1/track?q=" & (Songname))
    ElseIf mode = 2 Then
        url = ("http://ws.spotify.com/search/1/artist?q=" & (Songname))
    ElseIf mode = 3 Then
        url = ("http://ws.spotify.com/album/1/track?q=" & (Songname))
    End If
    i think the ns was the problem...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    16

    Re: Help with XML in visual basic.

    Haha I've must've messed those up
    But, I only used mode 1 when testing anyways, but thanks for the heads up

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    16

    Re: Help with XML in visual basic.

    Quote Originally Posted by .paul. View Post
    try this:
    Mate, you are a god! Thank you so much!!!
    Now I understand how to use these in the right way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width