Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I have translated some code from C# to VB. I found the code here http://www.codeproject.com/KB/webservices/FaisonMusicExplorer1.aspx?msg=4036510#xx4036510xx
I try to retrieve the artist ID from the musicbrainz server, but i get an error when i use my translated code in VB.

When I look at the string value xpath in C# it holds the value "//mb:artist[mb:name="The Beatles\]". After that it retrieves the artist ID without any problem.

Now, when I try to do the same in VB it does not retrieve the artist ID.

The first code-block is the one I found, the second is my translation of the first code-block. I can't see what i'm doing wrong...

C#
// find the artist ID of a given artist.
   public static string FindArtistId(string name)
   {
     try
     {
       string uri = string.Format("http://musicbrainz.org/ws/1/artist/?query={0}&type=xml", name);
       XPathDocument doc = new XPathDocument(uri);
       XPathNavigator nav = doc.CreateNavigator();
       XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
       nsmgr.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-1.0#");
       string xpath = string.Format("//mb:artist[mb:name=\"{0}\"]", name);   // The artist name is case-sensitive!
       XPathNodeIterator ni = nav.Select(xpath, nsmgr);
       if (!ni.MoveNext()) return null;
       XPathNavigator current = ni.Current;
       return current.GetAttribute("id", nsmgr.DefaultNamespace);
     }
     catch (WebException wex)
     {
       MessageBox.Show(string.Format("A communication error occurred ({0}). The MusicBrainz server might be down.", wex.Status), "Couldn't resolve artist name");
       return null;
     }
     catch (Exception ex)
     {
       MessageBox.Show(string.Format("An error occurred ({0}). The error may have been caused by bad data from the MusicBrainz server.", ex.Message), "Couldn't resolve artist name");
       return null;
     }
   }


VB
Public Function FindArtistId(ByVal name As String) As String
        Try
            Dim uri As String = String.Format("http://musicbrainz.org/ws/1/artist/?query={0}&type=xml", name)
            Dim doc As XPathDocument = New XPathDocument(uri)
            Dim nav As XPathNavigator = doc.CreateNavigator()
            Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
            nsmgr.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-1.0#")
            Dim xpath As String = String.Format("//mb:artist[mb:name=""{0}""]", name) 'The artist name is case-sensitive!
            Dim ni As XPathNodeIterator = nav.Select(xpath, nsmgr)
            If ni.MoveNext() Then Return Nothing
            Dim current As XPathNavigator = ni.Current
            Return current.GetAttribute("id", nsmgr.DefaultNamespace)
        Catch wex As WebException
            MessageBox.Show(String.Format("A communication error occurred ({0}). The MusicBrainz server might be down.", wex.Status), "Couldn't resolve artist name")
            Return Nothing
        Catch ex As Exception
            MessageBox.Show(String.Format("An error occurred ({0}). The error may have been caused by bad data from the MusicBrainz server.", ex.Message), "Couldn't resolve artist name")
            Return Nothing
        End Try
    End Function
Posted
Updated 30-Sep-11 8:02am
v3

1 solution

C#
if (!ni.MoveNext()) return null;

is not the same as
VB
If ni.MoveNext() Then Return Nothing

try
VB
If Not ni.MoveNext() Then
    Return Nothing
End If
It's the Not that is important, but I would add the End If anyway.
 
Share this answer
 
Comments
André Kraak 30-Sep-11 14:17pm    
Nice catch, my 5
Mandar IJnen 1-Oct-11 10:58am    
So the exclamation mark (!), as used in C#, is the same as the Not operator VB.
Learning every day...

Thanks! Problem solved.
OriginalGriff 1-Oct-11 11:43am    
That's the one! You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900