简体   繁体   中英

Process MusicBrainz Web Service

I know MusicBrainz has a version 2 available but v1 is the only way I got this far. (This is 1 return, it is usually hundreds...): 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>

I am able to get all albums returned with this :

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);
            }
        }

How can I also get the release id for each album?

As you already selected the release-node, you just need to get that id -attribute:

var q = from r in results.Descendants(ns + "release")
        select new 
        { 
            Title = r.Element(ns + "title").Value, 
            Release = r.Attribute("id").Value 
        };

Here the class you can use to store your data

class Release 
{
    public Guid Id {get; set;}
    public string Title {get; set;}
}

This is how you can retrieve the collection of Release from your respond.

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 
    };

Works great! Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM