Search code examples
c#xmlsubsonicrestsharp

RestSharp: ItemChoice always null


I'm new to RestSharp and trying to access my subsonic server through the subsonic-api. Every Response in wrapped in a subsonic-response Tag, e.g.

<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 generated the following classes for serialization:

 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 ----

When I'm trying to deserialize into a Response-Object the field Item is always null and ItemElementName gets the first value from the enum ItemChoiceType but version and status are correctly set. The retrieved xml-output looks like expected (see sample). What works though is deserializing into ArtistsID3 but then I loose the status.

Here's the code:

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;
     }

I'd be glad if somebody could point me in the right direction as I think the right way should be accessing the data through an Response object. Really don't know how to debug this not expected behaviour.


Solution

  • try this

    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; }
        }
    }
    ​