简体   繁体   English

使用RestSharp从MusicBrainz反序列化XML

[英]Deserializing XML from MusicBrainz using RestSharp

I'm trying to retrieve XML from MusicBrainz's artist search. 我正在尝试从MusicBrainz的歌手搜索中检索XML。 The XML is returned correctly from the server, but I cannot get it to deserialize to my classes. XML是从服务器正确返回的,但是我无法将其反序列化到我的类中。 Here is a sample of the 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>

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. response.Data对象为空。 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? 之前有人用C#成功地反序列化MusicBrainz中的数据吗?

Has anyone successfully deserialized data from MusicBrainz in C# before? 之前有人用C#成功地反序列化MusicBrainz中的数据吗?

According to the MusicBrainz Web Service Documentation there is a C# wrapper available. 根据MusicBrainz Web服务文档 ,可以使用C#包装器

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 . 该库正在使用System.Net.WebRequest而不是RestSharp I personally don't have experience with any of these, so I can't help you with that specific C# problem. 我个人没有任何经验,因此我无法帮助您解决特定的C#问题。 Being able to handle the MB XML in any way is the goal though, right? 目标是能够以任何方式处理MB XML,对吗?

EDIT: WebRequestHelper.cs is doing what you are trying to do (using WebRequest and XmlSerializer ). 编辑: WebRequestHelper.cs正在做您想做的事情(使用WebRequestXmlSerializer )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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