RestSharp 使用 SubSonic REST Api
我目前正在编写 ac# 库,以便在 Windows、iOS、Android 上使用跨平台。我正在使用 Rest 服务,并且在从响应返回的对象方面遇到一些问题。 我使用 RestSharp 进行 api 调用。我使用 Xsd2Code.NET 从 api 提供的 xsd 生成我的类。
问题是响应被包装在
中。我想要的物品就包含在里面。如果我将类型作为 List
传入,RestSharp 会尝试解析并执行操作,但其中的项目不会填充到 NowPlaying 对象中。我为 NowPlaying 生成了序列化/反序列化方法,但由于
作为根元素,因此引发了异常。有没有办法删除
?我尝试使用response.RootElement =“subsonic-response”进行RestSharp调用,但不起作用。请参阅下面的回复。任何帮助都会很棒。
RestResponse:
<?xml version="1.0" encoding="UTF-8"?>
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.7.0">
<nowPlaying>
<entry id="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b31325c38372e2044616e63652044616e63652028445542535445502052454d495829202d20426967205365616e2e6d7033" parent="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b3132"
title="Dance Dance (DUBSTEP REMIX) - Big Sean"
isDir="false"
album="M3 MIXTAPE (MEMBA. ME. MAAD)"
artist="DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE"
duration="67"
bitRate="192"
year="2012"
size="1615419"
suffix="mp3"
contentType="audio/mpeg"
isVideo="false"
coverArt="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b31325c444a20434852495354554646202d204d454d4241204d45204d4141442046524f4e542e6a7067"
path="Jan 2k12/DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE PRESENTS M3 (MEMBA ME MAAD) 2K12/DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE PRESENTS M3 (MEMBA ME MAAD) 2K12/87. Dance Dance (DUBSTEP REMIX) - Big Sean.mp3"
username="admin"
playerId="2"
playerName="subAir"
minutesAgo="0"/>
</nowPlaying>
</subsonic-response>
生成的类:
public partial class NowPlaying : EntityBase<NowPlaying>
{
[EditorBrowsable(EditorBrowsableState.Never)]
private List<NowPlayingEntry> entryField;
public List<NowPlayingEntry> entry
{
get
{
if ((this.entryField == null))
{
this.entryField = new List<NowPlayingEntry>();
}
return this.entryField;
}
set
{
if ((this.entryField != null))
{
if ((entryField.Equals(value) != true))
{
this.entryField = value;
this.OnPropertyChanged("entry");
}
}
else
{
this.entryField = value;
this.OnPropertyChanged("entry");
}
}
}
}
我调用的方法从其余服务获取 NowPlaying
public NowPlaying getNowPlaying()
{
NowPlaying playing;
try
{
var request = new RestRequest();
request.Resource = "getNowPlaying.view";
playing = SendRequest<NowPlaying>(request);
}
catch (Exception ex)
{
throw ex;
}
return playing;
}
I am currently writing a c# library to use cross platform on windows, iOS, Android. I am consuming a Rest service and having some trouble with the objects being returned from the response.
I am using RestSharp for the api calls. I used Xsd2Code.NET to generate my classes from an xsd provided by the api.
Problem is the responses are wrapping in a <subsonic-response>
. The item I want is contained within. RestSharp tries to parse and does if I pass in the type as a List<NowPlaying>
but the items within that do not get populated to the NowPlaying object. I generated the serialize/deserialize methods for NowPlaying but due to the <subsonic-response>
as the root element an exception is thrown. Is there a way to remove <subsonic-response>
? I tried response.RootElement = "subsonic-response" for the RestSharp call but does not work. See response below. Any help would be great.
RestResponse:
<?xml version="1.0" encoding="UTF-8"?>
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.7.0">
<nowPlaying>
<entry id="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b31325c38372e2044616e63652044616e63652028445542535445502052454d495829202d20426967205365616e2e6d7033" parent="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b3132"
title="Dance Dance (DUBSTEP REMIX) - Big Sean"
isDir="false"
album="M3 MIXTAPE (MEMBA. ME. MAAD)"
artist="DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE"
duration="67"
bitRate="192"
year="2012"
size="1615419"
suffix="mp3"
contentType="audio/mpeg"
isVideo="false"
coverArt="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b31325c444a20434852495354554646202d204d454d4241204d45204d4141442046524f4e542e6a7067"
path="Jan 2k12/DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE PRESENTS M3 (MEMBA ME MAAD) 2K12/DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE PRESENTS M3 (MEMBA ME MAAD) 2K12/87. Dance Dance (DUBSTEP REMIX) - Big Sean.mp3"
username="admin"
playerId="2"
playerName="subAir"
minutesAgo="0"/>
</nowPlaying>
</subsonic-response>
A class that was generated:
public partial class NowPlaying : EntityBase<NowPlaying>
{
[EditorBrowsable(EditorBrowsableState.Never)]
private List<NowPlayingEntry> entryField;
public List<NowPlayingEntry> entry
{
get
{
if ((this.entryField == null))
{
this.entryField = new List<NowPlayingEntry>();
}
return this.entryField;
}
set
{
if ((this.entryField != null))
{
if ((entryField.Equals(value) != true))
{
this.entryField = value;
this.OnPropertyChanged("entry");
}
}
else
{
this.entryField = value;
this.OnPropertyChanged("entry");
}
}
}
}
My method I am calling to get the NowPlaying from the rest service
public NowPlaying getNowPlaying()
{
NowPlaying playing;
try
{
var request = new RestRequest();
request.Resource = "getNowPlaying.view";
playing = SendRequest<NowPlaying>(request);
}
catch (Exception ex)
{
throw ex;
}
return playing;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了我的问题。最终结果是不让 RestSharp 反序列化我的对象。获取 RestResponse 内容,即 xml 响应。然后反序列化 api 的 Response 对象。抓住response.item并将其投射到我的NowPlaying对象,所有数据都在对象中。
Solved my problem. End result was to not let RestSharp deserialize my object. Get the RestResponse content which is the the xml response. Then deserialize the Response object for the api. Grabbed the response.item and casted it to my NowPlaying object and all data was in object.
与该架构匹配的 C# 类的一般结构如下:
然后您可以调用 Execute() 并且应该填充它。
The general structure for a C# class that will match that schema is this:
Then you can call
Execute<SubsonicResource>()
and it should be populated.