我正在尝试从 MusicBrainz 的艺术家搜索中检索 XML。XML 从服务器正确返回,但我无法将其反序列化为我的类。这是 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>
这是我的数据类:
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; }
}
}
这是我的控制器:
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;
}
}
}
response.Data 对象为空。我已经彻底研究了这个问题,并尝试了我能想到的一切。我忽略了什么吗?以前有没有人在 C# 中成功反序列化过 MusicBrainz 的数据?