0

我刚开始使用 RestSharp 并且使用 NextFlix API 已经能够成功地创建我的 Oauth 令牌并从 NetFlix 请求数据。但由于某种原因,我似乎无法创建我的对象以正确匹配返回的 XML。

我的代码:

    var client2 = new RestClient("http://api-public.netflix.com")
        {
            Authenticator = OAuth1Authenticator.ForProtectedResource(MyOauth.ConsumerKey, MyOauth.ConsumerSecret, MyOauth.OauthToken, MyOauth.OauthTokenSecret)
        };

        var request = new RestRequest("/catalog/titles/autocomplete");
        request.AddParameter("term", "star wars");
        var searchResults = client2.Execute<CatalogList>(request);

我尝试的对象创建:

  [XmlRoot("autocomplete")]
    public class CatalogList
    {
        public List<AutoCompleteItem> Titles { get; set; }
    }

    [XmlRoot("autocomplete_item")]
    public class AutoCompleteItem
    {
        [XmlElement("title short")]
        public string Title { get; set; }

        [XmlElement("short")]
        public string ShortName { get; set; }
    }

serachresults 返回 20 个标题,我的 List 有 20 个条目,但是 AutoCompleteItem 始终为空。我已经更改了元素名称、属性等,但从未得到正确的组合。

返回的 XML 如下所示:

<?xml version="1.0" standalone="yes" ?> 
<autocomplete>
<url_template>http://api-public.netflix.com/catalog/titles/autocomplete?{-join|&|term}   </url_template> 
<autocomplete_item>
<title short="Star Wars: Episode II: Attack of the Clones" /> 
</autocomplete_item>

我在这里想念什么?

谢谢,

杰森

4

1 回答 1

0
[XmlRoot("autocomplete")]
public class AutocompleteList
{
    [XmlElement("url_template")]
    public string UrlTemplate { get; set; }

    [XmlElement("autocomplete_item")]
    public List<AutocompleteItem> Items { get; set; }
}

public class AutocompleteItem
{
    [XmlElement("title")]
    public Title ItemTitle { get; set; }
}

public class Title
{
    [XmlAttribute("short")]
    public string Short { get; set; }
}

内部Title类型是捕获 XML 属性所必需的short=

string xml = @"<?xml version=""1.0"" standalone=""yes"" ?>
<autocomplete>
    <url_template>http://api-public.netflix.com/catalog/titles/autocomplete?{-join|&amp;|term}</url_template>
    <autocomplete_item>
        <title short=""Star Wars: Episode II: Attack of the Clones"" />
    </autocomplete_item>
</autocomplete>";

var reader = new StringReader(xml);
var ser = new XmlSerializer(typeof(AutocompleteList));

var result = (AutocompleteList) ser.Deserialize(reader);

这会产生与

var result = new AutocompleteList
{
    UrlTemplate = "http://api-public.netflix.com/catalog/titles/autocomplete?{-join|&|term}",
    Items = new List<AutocompleteItem>
    {
        new AutocompleteItem
        {
            ItemTitle = new Title
            {
                Short = "Star Wars: Episode II: Attack of the Clones",
            }
        },
    },
}
于 2012-09-14T18:06:47.290 回答