0

我目前正在编写 ac# 库以在 Windows、iOS、Android 上使用跨平台。我正在使用 Rest 服务,并且从响应返回的对象遇到了一些问题。我正在使用 RestSharp 进行 api 调用。我使用 Xsd2Code.NET 从 api 提供的 xsd 生成我的类。

问题是响应包装在<subsonic-response>. 我想要的项目包含在里面。RestSharp 尝试解析,如果我将类型作为 a 传递,List<NowPlaying>但其中的项目不会填充到 NowPlaying 对象中。我为 NowPlaying 生成了序列化/反序列化方法,但由于<subsonic-response>作为根元素引发了异常。有没有办法去除<subsonic-response>?我为 RestSharp 调用尝试了 response.RootElement = "subsonic-response" 但不起作用。请参阅下面的回复。任何帮助都会很棒。

休息响应:

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

    }
4

2 回答 2

0

将匹配该架构的 C# 类的一般结构是这样的:

public class SubsonicResource {
    public List<entry> NowPlaying { get; set; }
}

public class entry {
    public string Id { get; set; }
    public string Path { get; set; }
    public string Username { get; set; }
    ... 
}

然后你可以打电话Execute<SubsonicResource>(),它应该被填充。

于 2012-02-02T17:14:09.767 回答
0

解决了我的问题。最终结果是不让 RestSharp 反序列化我的对象。获取作为 xml 响应的 RestResponse 内容。然后反序列化 api 的 Response 对象。抓住 response.item 并将其投射到我的 NowPlaying 对象中,所有数据都在对象中。

string xml = SendRequest(request);
var res = Response.Deserialize(xml);
playing =(NowPlaying)res.Item;
于 2012-01-30T06:06:53.223 回答