从MusicBrainz检索数据(从C#转换代码) [英] Retrieve data from MusicBrainz (code translated from C#)
问题描述
嗨!
我已经将一些代码从C#转换为VB.我在这里找到了代码 http://www.codeproject.com/KB/webservices/FaisonMusicExplorer1.aspx?msg=4036510#xx4036510xx
我尝试从musicbrainz服务器检索艺术家ID,但是在VB中使用翻译后的代码时出现错误.
当我在C#中查看字符串值xpath时,它的值是"//mb:artist [mb:name =" The Beatles \] .此后,它将毫无问题地检索艺术家ID.
现在,当我尝试在VB中执行相同操作时,它不会检索艺术家ID.
第一个代码块是我找到的代码块,第二个是我对第一个代码块的翻译.我看不到我在做什么错...
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...
// 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;
}
}
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
推荐答案
if (!ni.MoveNext()) return null;
与
不同
is not the same as
If ni.MoveNext() Then Return Nothing
尝试
try
If Not ni.MoveNext() Then
Return Nothing
End If
重要的是Not
,但我要补充一点仍然是 End If
.
It''s the Not
that is important, but I would add the End If
anyway.
这篇关于从MusicBrainz检索数据(从C#转换代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!