1

在使用 Google Elevations API 成功试用 RestSharp 后,我想在 Flickr API 上试用它。对于 Elevations API,我创建了一组 POCO 类,这些类是从对 Elevation 查询的 XML 响应中派生的。我想对 Flickr API 做同样的事情。(我知道有很棒的 FlickrNet,但我想尝试使用 RestSharp)。

我有一个正在运行的 POCO 类版本,但我错过了一些信息。

这是 XML 中的响应:

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
  <photos page="1" pages="2" perpage="100" total="117">
    <photo id="8626881695" owner="62679167@N05" secret="e82b58b22f" server="8519" farm="9" title="IMG_3149.JPG" ispublic="1" isfriend="0" isfamily="0" latitude="51.507431" longitude="-0.122815" accuracy="16" context="0" place_id="I.7fkAxTUrq7FstgaA" woeid="20094318" geo_is_family="0" geo_is_friend="0" geo_is_contact="0" geo_is_public="1" url_sq="http://farm9.staticflickr.com/8519/8626881695_e82b58b22f_s.jpg" height_sq="75" width_sq="75" url_t="http://farm9.staticflickr.com/8519/8626881695_e82b58b22f_t.jpg" height_t="100" width_t="100" url_s="http://farm9.staticflickr.com/8519/8626881695_e82b58b22f_m.jpg" height_s="240" width_s="240" url_q="http://farm9.staticflickr.com/8519/8626881695_e82b58b22f_q.jpg" height_q="150" width_q="150" url_m="http://farm9.staticflickr.com/8519/8626881695_e82b58b22f.jpg" height_m="500" width_m="500" url_n="http://farm9.staticflickr.com/8519/8626881695_e82b58b22f_n.jpg" height_n="320" width_n="320" url_z="http://farm9.staticflickr.com/8519/8626881695_e82b58b22f_z.jpg" height_z="640" width_z="640" url_c="http://farm9.staticflickr.com/8519/8626881695_e82b58b22f_c.jpg" height_c="800" width_c="800" url_l="http://farm9.staticflickr.com/8519/8626881695_e82b58b22f_b.jpg" height_l="1024" width_l="1024" url_o="http://farm9.staticflickr.com/8519/8626881695_a923d82fe6_o.jpg" height_o="2112" width_o="2112" />
    <photo id="8626886683" owner="62679167@N05" secret="f308e6462c" server="8240" farm="9" title="" ispublic="1" isfriend="0" isfamily="0" latitude="51.507236" longitude="-0.122338" accuracy="16" context="0" place_id="I.7fkAxTUrq7FstgaA" woeid="20094318" geo_is_family="0" geo_is_friend="0" geo_is_contact="0" geo_is_public="1" url_sq="http://farm9.staticflickr.com/8240/8626886683_f308e6462c_s.jpg" height_sq="75" width_sq="75" url_t="http://farm9.staticflickr.com/8240/8626886683_f308e6462c_t.jpg" height_t="100" width_t="100" url_s="http://farm9.staticflickr.com/8240/8626886683_f308e6462c_m.jpg" height_s="240" width_s="240" url_q="http://farm9.staticflickr.com/8240/8626886683_f308e6462c_q.jpg" height_q="150" width_q="150" url_m="http://farm9.staticflickr.com/8240/8626886683_f308e6462c.jpg" height_m="500" width_m="500" url_n="http://farm9.staticflickr.com/8240/8626886683_f308e6462c_n.jpg" height_n="320" width_n="320" url_z="http://farm9.staticflickr.com/8240/8626886683_f308e6462c_z.jpg" height_z="612" width_z="612" url_o="http://farm9.staticflickr.com/8240/8626886683_c98a1a9f02_o.jpg" height_o="612" width_o="612" />
    <photo id="8626885149" owner="62679167@N05" secret="4dde1595e4" server="8387" farm="9" title="IMG_3147.JPG" ispublic="1" isfriend="0" isfamily="0" latitude="51.509048" longitude="-0.124458" accuracy="16" context="0" place_id="aOppNH9QV7zH7w" woeid="15652" geo_is_family="0" geo_is_friend="0" geo_is_contact="0" geo_is_public="1" url_sq="http://farm9.staticflickr.com/8387/8626885149_4dde1595e4_s.jpg" height_sq="75" width_sq="75" url_t="http://farm9.staticflickr.com/8387/8626885149_4dde1595e4_t.jpg" height_t="100" width_t="100" url_s="http://farm9.staticflickr.com/8387/8626885149_4dde1595e4_m.jpg" height_s="240" width_s="240" url_q="http://farm9.staticflickr.com/8387/8626885149_4dde1595e4_q.jpg" height_q="150" width_q="150" url_m="http://farm9.staticflickr.com/8387/8626885149_4dde1595e4.jpg" height_m="500" width_m="500" url_n="http://farm9.staticflickr.com/8387/8626885149_4dde1595e4_n.jpg" height_n="320" width_n="320" url_z="http://farm9.staticflickr.com/8387/8626885149_4dde1595e4_z.jpg" height_z="640" width_z="640" url_c="http://farm9.staticflickr.com/8387/8626885149_4dde1595e4_c.jpg" height_c="800" width_c="800" url_l="http://farm9.staticflickr.com/8387/8626885149_4dde1595e4_b.jpg" height_l="1024" width_l="1024" url_o="http://farm9.staticflickr.com/8387/8626885149_8f928ce11a_o.jpg" height_o="2112" width_o="2112" />
    <photo id="8626825995" owner="24343055@N03" secret="0ba9a79f43" server="8262" farm="9" title="Heroes Salute" ispublic="1" isfriend="0" isfamily="0" latitude="51.503119" longitude="-0.149366" accuracy="16" context="0" place_id="yy7TEQtVUbhVOw" woeid="43246" geo_is_family="0" geo_is_friend="0" geo_is_contact="0" geo_is_public="1" url_sq="http://farm9.staticflickr.com/8262/8626825995_0ba9a79f43_s.jpg" height_sq="75" width_sq="75" url_t="http://farm9.staticflickr.com/8262/8626825995_0ba9a79f43_t.jpg" height_t="75" width_t="100" url_s="http://farm9.staticflickr.com/8262/8626825995_0ba9a79f43_m.jpg" height_s="180" width_s="240" url_q="http://farm9.staticflickr.com/8262/8626825995_0ba9a79f43_q.jpg" height_q="150" width_q="150" url_m="http://farm9.staticflickr.com/8262/8626825995_0ba9a79f43.jpg" height_m="375" width_m="500" url_n="http://farm9.staticflickr.com/8262/8626825995_0ba9a79f43_n.jpg" height_n="240" width_n="320" url_z="http://farm9.staticflickr.com/8262/8626825995_0ba9a79f43_z.jpg" height_z="480" width_z="640" url_c="http://farm9.staticflickr.com/8262/8626825995_0ba9a79f43_c.jpg" height_c="600" width_c="800" url_l="http://farm9.staticflickr.com/8262/8626825995_0ba9a79f43_b.jpg" height_l="768" width_l="1024" url_o="http://farm9.staticflickr.com/8262/8626825995_309be230ce_o.jpg" height_o="2313" width_o="3085" />
    <photo id="8626826765" owner="24343055@N03" secret="a14c1a395c" server="8526" farm="9" title="Long Overdue" ispublic="1" isfriend="0" isfamily="0" latitude="51.503173" longitude="-0.149318" accuracy="16" context="0" place_id="yy7TEQtVUbhVOw" woeid="43246" geo_is_family="0" geo_is_friend="0" geo_is_contact="0" geo_is_public="1" url_sq="http://farm9.staticflickr.com/8526/8626826765_a14c1a395c_s.jpg" height_sq="75" width_sq="75" url_t="http://farm9.staticflickr.com/8526/8626826765_a14c1a395c_t.jpg" height_t="100" width_t="75" url_s="http://farm9.staticflickr.com/8526/8626826765_a14c1a395c_m.jpg" height_s="240" width_s="180" url_q="http://farm9.staticflickr.com/8526/8626826765_a14c1a395c_q.jpg" height_q="150" width_q="150" url_m="http://farm9.staticflickr.com/8526/8626826765_a14c1a395c.jpg" height_m="500" width_m="375" url_n="http://farm9.staticflickr.com/8526/8626826765_a14c1a395c_n.jpg" height_n="320" width_n="240" url_z="http://farm9.staticflickr.com/8526/8626826765_a14c1a395c_z.jpg" height_z="640" width_z="480" url_c="http://farm9.staticflickr.com/8526/8626826765_a14c1a395c_c.jpg" height_c="800" width_c="600" url_l="http://farm9.staticflickr.com/8526/8626826765_a14c1a395c_b.jpg" height_l="1024" width_l="768" url_o="http://farm9.staticflickr.com/8526/8626826765_7db667a6ed_o.jpg" height_o="3121" width_o="2340" />
    <photo id="8626843195" owner="24343055@N03" secret="bf3c7309a5" server="8104" farm="9" title="Bus Bonus" ispublic="1" isfriend="0" isfamily="0" latitude="51.503319" longitude="-0.149323" accuracy="16" context="0" place_id="yy7TEQtVUbhVOw" woeid="43246" geo_is_family="0" geo_is_friend="0" geo_is_contact="0" geo_is_public="1" url_sq="http://farm9.staticflickr.com/8104/8626843195_bf3c7309a5_s.jpg" height_sq="75" width_sq="75" url_t="http://farm9.staticflickr.com/8104/8626843195_bf3c7309a5_t.jpg" height_t="100" width_t="75" url_s="http://farm9.staticflickr.com/8104/8626843195_bf3c7309a5_m.jpg" height_s="240" width_s="180" url_q="http://farm9.staticflickr.com/8104/8626843195_bf3c7309a5_q.jpg" height_q="150" width_q="150" url_m="http://farm9.staticflickr.com/8104/8626843195_bf3c7309a5.jpg" height_m="500" width_m="375" url_n="http://farm9.staticflickr.com/8104/8626843195_bf3c7309a5_n.jpg" height_n="320" width_n="240" url_z="http://farm9.staticflickr.com/8104/8626843195_bf3c7309a5_z.jpg" height_z="640" width_z="480" url_c="http://farm9.staticflickr.com/8104/8626843195_bf3c7309a5_c.jpg" height_c="800" width_c="600" url_l="http://farm9.staticflickr.com/8104/8626843195_bf3c7309a5_b.jpg" height_l="1024" width_l="768" url_o="http://farm9.staticflickr.com/8104/8626843195_601d42bdd2_o.jpg" height_o="3067" width_o="2300" />
    <photo id="8626786831" owner="24664325@N06" secret="c30d6881bf" server="8261" farm="9" title="Breaking the girl" ispublic="1" isfriend="0" isfamily="0" latitude="51.508234" longitude="-0.128188" accuracy="16" context="0" place_id="1EPGyGtTUrrDXJmGCA" woeid="20094364" geo_is_family="0" geo_is_friend="0" geo_is_contact="0" geo_is_public="1" url_sq="http://farm9.staticflickr.com/8261/8626786831_c30d6881bf_s.jpg" height_sq="75" width_sq="75" url_t="http://farm9.staticflickr.com/8261/8626786831_c30d6881bf_t.jpg" height_t="64" width_t="100" url_s="http://farm9.staticflickr.com/8261/8626786831_c30d6881bf_m.jpg" height_s="155" width_s="240" url_q="http://farm9.staticflickr.com/8261/8626786831_c30d6881bf_q.jpg" height_q="150" width_q="150" url_m="http://farm9.staticflickr.com/8261/8626786831_c30d6881bf.jpg" height_m="322" width_m="500" url_n="http://farm9.staticflickr.com/8261/8626786831_c30d6881bf_n.jpg" height_n="206" width_n="320" url_z="http://farm9.staticflickr.com/8261/8626786831_c30d6881bf_z.jpg" height_z="412" width_z="640" url_c="http://farm9.staticflickr.com/8261/8626786831_c30d6881bf_c.jpg" height_c="515" width_c="800" url_l="http://farm9.staticflickr.com/8261/8626786831_c30d6881bf_b.jpg" height_l="644" width_l="1000" />
...cut for brevity
  </photos>
</rsp>

以下是有效的 POCO 类,但缺少信息:

public class Reply
  {
    public string stat {get;set;}
    public List<Photo> photos {get;set;}
  }

  public class Photo
  {
    public string id {get;set;}
    public string owner {get;set;}
    public string secret {get;set;}
    public string server {get;set;}
    public string farm {get;set;}
    public string title {get;set;}
    public string ispublic {get;set;}
    public string isfriend {get;set;}
    public string isfamily {get;set;}
    public string latitude {get;set;}
    public string longitude {get;set;}
    public string accuracy {get;set;}
    public string context {get;set;}
    public string place_id {get;set;}
    public string woeid {get;set;}
    public string geo_is_family {get;set;}
    public string geo_is_contact {get;set;}
    public string geo_is_public {get;set;}
    public string url_sq {get;set;}
    public string height_sq {get;set;}
    public string width_sq {get;set;}
    public string url_t {get;set;}
    public string height_t {get;set;}
    public string width_t {get;set;}
    public string url_s {get;set;}
    public string height_s {get;set;}
    public string width_s {get;set;}
    public string url_q {get;set;}
    public string height_q {get;set;}
    public string width_q {get;set;}
    public string url_m {get;set;}
    public string height_m {get;set;}
    public string width_m {get;set;}
    public string url_n {get;set;}
    public string height_n {get;set;}
    public string width_n {get;set;}
    public string url_z {get;set;}
    public string height_z {get;set;}
    public string width_z {get;set;}
    public string url_c {get;set;}
    public string height_c {get;set;}
    public string width_c {get;set;}
    public string url_l {get;set;}
    public string height_l {get;set;}
    public string width_l {get;set;}
    public string url_o {get;set;}
    public string height_o {get;set;}
    public string width_o {get;set;}

  }

我还想检索照片元素中的属性:

<photos page="1" pages="2" perpage="100" total="117">

对于上述类,我看不到获取这些属性的合乎逻辑的方法,因为我从未定义它们。我尝试了以下类(照片类与上面相同):

 public class Reply
  {
    public string stat {get;set;}
    public Photos photos {get;set;}

  }
  public class Photos
  {
    public string page {get;set;}
    public string pages {get;set;}
    public string perpage {get;set;}
    public string total {get;set;}
    public List<Photo> photoList {get;set;}
  }

当我尝试访问时response.Data.photos.page出现Object reference not set to an instance of an object错误。

任何人都可以阐明如何设计能够访问照片元素中的属性以及能够检索照片列表的类吗?

4

1 回答 1

0

RestSharp 默认不反序列化属性。github.com/restsharp/RestSharp/wiki/Deserialization 似乎区别在于,如果属性是一个没有其他内联内容的元素(即照片),则可以对属性进行反序列化。具有属性的元素和其中包含的其他元素似乎需要自定义反序列化器。相反,我使用 Xml.Linq 来访问照片元素属性:

System.Xml.Linq.XDocument xDoc = System.Xml.Linq.XDocument.Parse(response.Content); 
IEnumerable<int> page = 
   from item in xDoc.Descendants("photos") 
   select (int) item.Attribute("page"); etc...
于 2013-04-09T06:47:48.613 回答