我知道MusicBrainz有一个版本2可用,但v1是我走到这一步的唯一方法。(这是 1 个返回,通常是数百个... .XML:
<metadata xmlns="http://musicbrainz.org/ns/mmd-1.0#" xmlns:ext="http://musicbrainz.org/ns/ext-1.0#">
<release-list count="928" offset="0">
<release id="bea5602c-53bc-4416-af49-238aae51e8ea" type="Live Bootleg" ext:score="100">
<title>Seattle 1988</title>
<text-representation language="ENG" script="Latn" />
<artist id="5b11f4ce-a62d-471e-81fc-a69a8278c7da">
<name>Nirvana</name>
<sort-name>Nirvana</sort-name>
</artist>
<release-event-list>
<event format="CD" />
</release-event-list>
<disc-list count="0" />
<track-list count="10" />
</release>
</release-list>
</metadata>
我能够用这个返回所有专辑:
client = new WebClient();
client.OpenReadCompleted += client_OpenReadCompleted;
// Call public web service.
string requestUri =
"http://musicbrainz.org/ws/1/release/?limit=100&type=xml&artist={0}";
client.OpenReadAsync(
new Uri(String.Format(
requestUri, "Jimi Hendrix")));
private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
// Process returned data.
XElement results;
albums = new List<string>();
if (e.Error != null)
{ return; }
else
{
XNamespace ns =
@"http://musicbrainz.org/ns/mmd-1.0#";
results = XElement.Load(e.Result);
var q = from r in results.Descendants(ns + "release")
select new { Title = r.Element(ns + "title").Value };
foreach (var i in q)
{
albums.Add(i.Title);
Console.WriteLine(i.Title);
}
}
如何获取每张专辑的发行 ID?
由于您已经选择了发布节点,因此您只需要获取该id
属性:
var q = from r in results.Descendants(ns + "release")
select new
{
Title = r.Element(ns + "title").Value,
Release = r.Attribute("id").Value
};
这里可以用来存储数据的类
class Release
{
public Guid Id {get; set;}
public string Title {get; set;}
}
这是从响应中检索Release
集合的方法。
XNamespace ns = "http://musicbrainz.org/ns/mmd-1.0#";
var doc = XElement.Load(e.Result);
IEnumerable<Release> res = doc.Descendants(ns + "release")
.Select(r => new Release{
Id = (Guid)r.Attribute("id"),
Title = (string)r.Element(ns + "title")
});
var q = from r in results.Descendants(ns + "release")
select new
{
Title = r.Element(ns + "title").Value,
Release = r.Attribute("id").Value
};
效果很好!谢谢