|
-
Mar 23rd, 2013, 01:32 PM
#1
Thread Starter
Junior Member
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
-
Mar 23rd, 2013, 06:19 PM
#2
Frenzied Member
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
-
Mar 23rd, 2013, 06:43 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 23rd, 2013, 06:47 PM
#4
Re: Help with XML in visual basic.
 Originally Posted by billboy
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 23rd, 2013, 07:54 PM
#5
Thread Starter
Junior Member
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
-
Mar 23rd, 2013, 07:55 PM
#6
Thread Starter
Junior Member
Re: Help with XML in visual basic.
 Originally Posted by .paul.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|