6

我正在使用 restsharp 并遇到了问题。由于我使用的是返回 xml 数据的 google api,因此我遇到了名称冲突。

例如,从 google 联系人 api 返回所有“组”和“所有联系人”都具有“提要”的根节点,但其中的数据不同。

所以我做了这个

[XmlRoot(ElementName = "feed")]
public class GroupFeed
{
    public string Id { get; set; }
    public DateTime Updated { get; set; }
    public string Title { get; set; }
    public int TotalResults { get; set; }
    public int StartIndex { get; set; }
    public int ItemsPerPage { get; set; }
    [XmlElement(ElementName="Entry")]
    public List<GroupEntry> Entries { get; set; }
}

通过使用 XmlRoot 属性,它在我使用 restsharp 时起作用,但它从不填充条目,即使它们是数据。

[XmlRoot(ElementName = "entry")]
public class GroupEntry
{
    public string Id { get; set; }
    public DateTime Updated { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

}

如果我将 GroupEntry 重命名为 Entry,则它会被填充。似乎它没有使用我的 XMLRoot 属性作为名称。

如您所见,我也尝试过使用 XmlElement ,但这也无济于事。

  client.ExecuteAsync<GroupFeed>(request, response =>
            {
                var test = response.Data;
                var d = "";
            });

这是原始 XML,不确定这是否有帮助。

 <?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gd="http://schemas.google.com/g/2005">
   <id>junk@gmail.com</id>
   <updated>2013-04-01T18:32:26.482Z</updated>
   <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#group" />
   <title type="text">xiao bao's Contact Groups</title>
   <link rel="alternate" type="text/html" href="http://www.google.com/" />
   <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="https://www.google.com/m8/feeds/groups/junk%40gmail.com/full" />
   <link rel="http://schemas.google.com/g/2005#post" type="application/atom+xml" href="https://www.google.com/m8/feeds/groups/junk%40gmail.com/full" />
   <link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="https://www.google.com/m8/feeds/groups/junk%40gmail.com/full/batch" />
   <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/groups/junk%40gmail.com/full?max-results=25" />
   <author>
      <name>xiao bao</name>
      <email>junk@gmail.com</email>
   </author>
   <generator version="1.0" uri="http://www.google.com/m8/feeds">Contacts</generator>
   <openSearch:totalResults>2</openSearch:totalResults>
   <openSearch:startIndex>1</openSearch:startIndex>
   <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
   <entry>
      <id>http://www.google.com/m8/feeds/groups/junk%40gmail.com/base/5a185f89922304</id>
      <updated>2013-04-01T18:31:35.784Z</updated>
      <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#group" />
      <title type="text">My Second Group</title>
      <content type="text">My Second Group</content>
      <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/groups/junk%40gmail.com/full/5a185f89922304" />
      <link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/groups/junk%40gmail.com/full/5a185f89922304/1364841095784001" />
   </entry>
   <entry>
      <id>http://www.google.com/m8/feeds/groups/junk%40gmail.com/base/37f569c88989718f</id>
      <updated>2013-03-01T18:54:05.085Z</updated>
      <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#group" />
      <title type="text">My Test Group</title>
      <content type="text">My Test Group</content>
      <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/groups/junk%40gmail.com/full/37f569c88989718f" />
      <link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/groups/junk%40gmail.com/full/37f569c88989718f/1362164045085001" />
   </entry>
</feed>
4

1 回答 1

1

使用 XmlArrayItem 代替 XmlElement 来指定数组中的元素。

//[XmlArray("Entries")] // if you need to change property name
[XmlArrayItem("Entry")]
public List<GroupEntry> Entries { get; set; }
于 2013-03-31T11:14:42.127 回答