我正在使用restsharp并遇到了问题.由于我使用google api返回xml数据,因此我遇到名称冲突.
例如,返回来自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; }
}
Run Code Online (Sandbox Code Playgroud)
通过使用XmlRoot属性,它在我使用restsharp时起作用,但它从不填充Entries,即使它们是数据.
[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; }
}
Run Code Online (Sandbox Code Playgroud)
如果我将GroupEntry重命名为Entry,则会填充它.似乎它不使用我的XMLRoot属性作为名称.
正如您所看到的,我也尝试过使用XmlElement,但这也没有做任何事情.
client.ExecuteAsync<GroupFeed>(request, response =>
{
var test = response.Data;
var d = "";
});
Run Code Online (Sandbox Code Playgroud)
这是原始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>
Run Code Online (Sandbox Code Playgroud)
使用 XmlArrayItem 代替 XmlElement 来指定数组中的元素。
//[XmlArray("Entries")] // if you need to change property name
[XmlArrayItem("Entry")]
public List<GroupEntry> Entries { get; set; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
603 次 |
| 最近记录: |