0

I'm trying to retrieve XML from MusicBrainz's artist search. The XML is returned correctly from the server, but I cannot get it to deserialize to my classes. Here is a sample of the XML:

<?xml version="1.0" standalone="yes"?>
<metadata created="2014-08-04T21:43:02.658Z"
xmlns="http://musicbrainz.org/ns/mmd-2.0#"
xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
  <artist-list count="1" offset="0">
    <artist id="4d263664-5418-4f6c-b46a-36f413044e73" type="Group"
    ext:score="100">
      <name>Pallbearer</name>
      <sort-name>Pallbearer</sort-name>
      <country>US</country>
      <area id="489ce91b-6658-3307-9877-795b68554c98">
        <name>United States</name>
        <sort-name>United States</sort-name>
      </area>
      <life-span>
        <begin>2008</begin>
        <ended>false</ended>
      </life-span>
    </artist>
  </artist-list>
</metadata>

Here is my class for the data:

using System.Collections.Generic;

namespace CelestialAPI.Models
{
    public class ArtistList
    {
        public List<Artist> Artists { get; set; }
    }

    public class Artist
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public Area Area { get; set; }
        public string Disambiguation { get; set; }
    }

    public class Area
    {
        public string Name { get; set; } 
    }
}

And here is my controller:

using System.Web.Http;
using System.Xml.Serialization;
using RestSharp;
using CelestialAPI.Models;

namespace CelestialAPI.Controllers
{
    public class SearchController : ApiController
    {
        [HttpGet]
        public ArtistList Artist(string query, int limit = 10, int offset = 0)
        {
            var searchAPI = new MusicBrainzAPI();
            return searchAPI.SearchByArtist(query, limit, offset);
        }
    }

    public class MusicBrainzAPI
    {
        readonly string _baseURL = "http://musicbrainz.org/ws/2";
        readonly string _userAgent = "Celestial/dev (andrew@inmyroom.org)";

        public ArtistList SearchByArtist(string query, int limit = 10, int offset = 0)
        {
            string endPoint = "artist";

            var rest = new RestClient(_baseURL);
            rest.UserAgent = _userAgent;

            var request = new RestRequest();
            request.XmlNamespace = "http://musicbrainz.org/ns/mmd-2.0#";
            request.Resource = endPoint + "/?query=" + query + "&limit=" + limit + "&offset=" + offset;

            var response = rest.Execute<ArtistList>(request);
            return response.Data;
        }

    }
}

The response.Data object is empty. I've researched this issue thoroughly, and tried everything I can think of. Am I overlooking something? Has anyone successfully deserialized data from MusicBrainz in C# before?

1 Answers1

0

Has anyone successfully deserialized data from MusicBrainz in C# before?

According to the MusicBrainz Web Service Documentation there is a C# wrapper available.

You can either just use the library or read through the code to get ideas how it can be done.

That library is using System.Net.WebRequest rather than RestSharp. I personally don't have experience with any of these, so I can't help you with that specific C# problem. Being able to handle the MB XML in any way is the goal though, right?

EDIT: WebRequestHelper.cs is doing what you are trying to do (using WebRequest and XmlSerializer).

JonnyJD
  • 2,593
  • 1
  • 28
  • 44