RestSharp:ItemChoice 始终为空

RestSharp: ItemChoice always null

我是 RestSharp 的新手,正在尝试通过 subsonic-api 访问我的亚音速服务器。 每个响应都包裹在亚音速响应标签中,例如

<subsonic-response xmlns="http://subsonic.org/restapi"
               status="ok" version="1.10.1">
<artists ignoredArticles="The El La Los Las Le Les">
    <index name="A">
        <artist id="5449" name="A-Ha" coverArt="ar-5449" albumCount="4"/>
        <artist id="5421" name="ABBA" coverArt="ar-5421" albumCount="6"/>
        <artist id="5432" name="AC/DC" coverArt="ar-5432" albumCount="15"/>
        <artist id="6633" name="Aaron Neville" coverArt="ar-6633" albumCount="1"/>
    </index>
    <index name="B">
        <artist id="5950" name="Bob Marley" coverArt="ar-5950" albumCount="8"/>
        <artist id="5957" name="Bruce Dickinson" coverArt="ar-5957" albumCount="2"/>
    </index>
</artists>

Xsd 生成以下 类 用于序列化:

 public partial class Response
{

    private object itemField;

    private ItemChoiceType itemElementNameField;

    private ResponseStatus statusField;

    private string versionField;

    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
    public object Item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public ItemChoiceType ItemElementName
    {
        get
        {
            return this.itemElementNameField;
        }
        set
        {
            this.itemElementNameField = value;
        }
    }

    public ResponseStatus status
    {
        get
        {
            return this.statusField;
        }
        set
        {
            this.statusField = value;
        }
    }

    public string version
    {
        get
        {
            return this.versionField;
        }
        set
        {
            this.versionField = value;
        }
    }
}

public partial class ArtistsID3
{

    private List<IndexID3> indexField;

    public ArtistsID3()
    {
        this.indexField = new List<IndexID3>();
    }

    public List<IndexID3> index
    {
        get
        {
            return this.indexField;
        }
        set
        {
            this.indexField = value;
        }
    }
}

 public enum ItemChoiceType
{

    /// <remarks/>
    album,

    /// <remarks/>
    albumList,

--- snip ----

当我尝试反序列化为 Response-Object 时,字段 Item 始终为 null,并且 ItemElementName 从枚举 ItemChoiceType 中获取第一个值,但版本和状态设置正确。检索到的 xml-输出看起来像预期的那样(参见示例)。 虽然有效的是反序列化为 ArtistsID3,但随后我失去了状态。

代码如下:

SubsonicApi api = new SubsonicApi();
var request = new RestRequest();
request.Resource = "getArtists.view";
request.AddParameter("v", "1.10.2");
request.AddParameter("c", "no name yet");
// Item always null!
// var response = api.Execute<Response>(request);
var response = api.Execute<ArtistsID3>(request);

public T Execute<T>(RestRequest request)
        where T : new()
    {
        var client = new RestClient();
        client.BaseUrl = new System.Uri(BaseUrl);
        client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
        var response = client.Execute<T>(request);
        Console.WriteLine("RESPONSE-DATA: " + response.Content);
        return response.Data;
     }

如果有人能给我指出正确的方向,我会很高兴,因为我认为正确的方法应该是通过 Response 对象访问数据。真的不知道如何调试这种意外行为。

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            SubsonicResponse subsonicResponse = new SubsonicResponse()
            {
                status = "ok",
                version = "1.10.1",
                artists = new Artists()
                {
                    ignoredArticles = "The El La Los Las Le Les",
                    indexes = new List<Index>() {
                        new Index() {
                            name = "A", artists = new List<Artist>() {
                                new Artist() { id = 5449, name = "A-Ha", converArt = "ar-5449", albumCount = 4},
                                new Artist() { id = 5221, name = "ABBA", converArt = "ar-5421", albumCount = 6},
                                new Artist() { id = 5432, name = "AC/DC", converArt = "ar-5432", albumCount = 15},
                                new Artist() { id = 6633, name = "Aaron Neville", converArt = "ar-5633", albumCount = 1}
                            }
                        },
                        new Index() {
                            name = "B", artists = new List<Artist>() {
                                new Artist() { id = 5950, name = "Bob Marley", converArt = "ar-5950", albumCount = 8},
                                new Artist() { id = 5957, name = "Bruce Dickinson", converArt = "ar-5957", albumCount = 2}
                            }
                        }

                    }
                }
            };
            XmlSerializer serializer = new XmlSerializer(typeof(SubsonicResponse));

            StreamWriter writer = new StreamWriter(FILENAME);
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "http://subsonic.org/restapi");

            serializer.Serialize(writer, subsonicResponse, ns);
            writer.Flush();
            writer.Close();
            writer.Dispose();

            XmlSerializer xs = new XmlSerializer(typeof(SubsonicResponse));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            SubsonicResponse newSResponse = (SubsonicResponse)xs.Deserialize(reader);

        }
    }
    [XmlRoot("subsonic-response", Namespace = "http://subsonic.org/restapi")]
    public class SubsonicResponse
    {
        [XmlAttribute("status")]
        public string status { get; set; }

        [XmlAttribute("version")]
        public string version { get; set; }

        [XmlElement("artists")]
        public Artists artists { get; set; }
    }

    [XmlRoot(ElementName = "artists")]
    public class Artists
    {
        [XmlAttribute("ignoredArticles")]
        public string ignoredArticles { get; set; }

        [XmlElement("index")]
        public List<Index> indexes { get; set; }
    }
    [XmlRoot("index")]
    public class Index
    {
        [XmlAttribute("name")]
        public string  name { get; set; }

        [XmlElement("artist")]
        public List<Artist> artists { get; set; }
    }
    [XmlRoot("artist")]
    public class Artist
    {
       [XmlAttribute("id")]
        public int  id { get; set; }

       [XmlAttribute("name")]
        public string  name { get; set; }

       [XmlAttribute("converArt")]
        public string  converArt { get; set; }

       [XmlAttribute("albumCount")]
        public int  albumCount { get; set; }
    }
}
​