处理 MusicBrainz Web 服务
Process MusicBrainz Web Service
我知道 MusicBrainz 有可用的版本 2,但 v1 是我走到这一步的唯一途径。
(这个是1return,一般是几百...):
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>
我可以用这个return编辑所有专辑:
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 您可以用来存储您的数据
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
};
效果很好!谢谢
我知道 MusicBrainz 有可用的版本 2,但 v1 是我走到这一步的唯一途径。 (这个是1return,一般是几百...): 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>
我可以用这个return编辑所有专辑:
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 您可以用来存储您的数据
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
};
效果很好!谢谢