我正在使用restsharp并遇到了问题。由于我使用的是返回xml数据的google api,因此我遇到名称冲突。
例如,返回google contact api中的所有“group”和“all contacts”都有一个“feed”的根节点,但其中包含不同的数据。
所以我做了这个
[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>
答案 0 :(得分:1)
使用XmlElement的XmlArrayItem instad指定数组中的元素。
//[XmlArray("Entries")] // if you need to change property name
[XmlArrayItem("Entry")]
public List<GroupEntry> Entries { get; set; }