2013-03-21 10 views
6

Używam restsharp i wpadłem na problem. Ponieważ używam aplikacji Google api, która zwraca dane xml, używam konfliktów nazw.Jak wykonać wewnętrzne węzły XML z RestSharp, gdy klasa ma inną nazwę?

Na przykład zwróć wszystkie "grupy" i "wszystkie kontakty" z api google google contact, oba mają węzeł główny "feed", ale inne dane w nim.

Więc zrobiłem to

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

Używając XmlRoot przypisują to działa, gdy używam restsharp ale nigdy nie zapełnia Wpisy chociaż ich jest dane.

Jeśli zmienię nazwę GroupEntry na Entry, zostanie ona wypełniona. Wygląda na to, że nie używa mojego atrybutu XMLRoot jako nazwy.

Jak widać, próbowałem również używać XmlElement, ale to również nie działa.

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

Tutaj jest surowy XML nie jest pewien, czy to pomoże.

<?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>ju[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> 

Odpowiedz

1

Zastosowanie XmlArrayItem instad z XmlElement określić element tablicy.

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

Nie, to nie działa ... Wciąż liczy się zero. – chobo2

+0

Masz niepoprawną strukturę w klasie GroupFeed. Masz tylko jeden wpis GroupEntry dla każdego kanału w twoim xml. Więc GroupFeed powinien mieć: [XmlElement (ElementName = "entry")] public GroupEntry Entrie {get; zestaw; } zamiast wpisów. –

+0

Opublikowałem xml. Może istnieć więcej niż jeden wpis. – chobo2