Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Inner XML Nodes with RestSharp when class has different name?

Tags:

xml

restsharp

I am using restsharp and ran into a problem. Since I am using the google api that returns back xml data I am running into name conflicts.

For instance return all "group" and "all contacts" from the google contact api both have a root node of "feed" but different data in it.

So I did this

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

By using the XmlRoot attribute it works when I use restsharp but it never populates Entries even though their is 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 then it gets populated. It seems like it does not use my XMLRoot attribute as the name.

As you can see I also tried using XmlElement as well but that does nothing as well.

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

Here is the raw XML 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>[email protected]</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>[email protected]</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>
like image 586
chobo2 Avatar asked Mar 21 '13 17:03

chobo2


1 Answers

Use XmlArrayItem instad of XmlElement to specify element in array.

//[XmlArray("Entries")] // if you need to change property name
[XmlArrayItem("Entry")]
public List<GroupEntry> Entries { get; set; }
like image 53
jan salawa Avatar answered Oct 26 '22 18:10

jan salawa