簡體   English   中英

RestSharp:ItemChoice始終為null

[英]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; }
    }
}
​

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM