RestSharp:ItemChoice始终为null(RestSharp: ItemChoice always null)

编程入门 行业动态 更新时间:2024-10-27 08:26:41
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对象访问数据。 真的不知道如何调试这种不期望的行为。

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.

最满意答案

尝试这个

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

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

更多推荐

本文发布于:2023-07-05 02:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1032121.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RestSharp   ItemChoice   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!