How to make internal XML nodes using RestSharp when the class has a different name?

I am using restsharp and ran into a problem. Since I use google api which returns xml data, I run name conflicts.

For example, to return all “groups” and “all contacts” from the google contact api, both have the root node “feed”, but different data in it.

So i did it

[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; } } 

Using the XmlRoot attribute, it works when I use restsharp, but it never fills the records, even if their data.

 [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; } } 

If I rename GroupEntry to Entry, it will be populated. It doesn't seem to be using my XMLRoot attribute as a name.

As you can see, I also tried using XmlElement, but that does nothing either.

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

Here the raw XML is not sure if this will help.

  <?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 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> 
+6
source share
1 answer

Use the XmlArrayItem instad XmlElement to indicate an element in an array.

 //[XmlArray("Entries")] // if you need to change property name [XmlArrayItem("Entry")] public List<GroupEntry> Entries { get; set; } 
+1
source

All Articles