2013-03-21 3 views
6

मैं restsharp का उपयोग कर रहा हूँ और एक समस्या में भाग गया। चूंकि मैं Google एपीआई का उपयोग कर रहा हूं जो एक्सएमएल डेटा वापस लौटाता है, मैं नाम विवादों में भाग रहा हूं।कक्षा के अलग-अलग नाम होने पर RestSharp के साथ आंतरिक XML नोड्स कैसे करें?

उदाहरण के लिए Google संपर्क एपीआई से सभी "समूह" और "सभी संपर्क" वापस लौटें, दोनों में "फ़ीड" का रूट नोड है लेकिन इसमें अलग-अलग डेटा हैं।

तो मैं इस

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

} 

यदि मैं Entry में GroupEntry का नाम बदलता हूं तो यह पॉप्युलेट हो जाता है। ऐसा लगता है कि यह मेरे XMLRoot विशेषता का नाम के रूप में उपयोग नहीं करता है।

जैसा कि आप देख सकते हैं कि मैंने XmlElement का उपयोग करने का भी प्रयास किया है, लेकिन यह कुछ भी नहीं करता है।

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

यहां कच्चे एक्सएमएल सुनिश्चित नहीं है कि इससे मदद मिलेगी।

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

उत्तर

1

सरणी में तत्व निर्दिष्ट करने के लिए XmlElement के XmlArrayItem इंस्टैड का उपयोग करें।

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

कोई काम नहीं करता .... अभी भी शून्य की गिनती है। – chobo2

+0

आपके समूह फ़ीड क्लास में आपके पास अवैध संरचना है। आपके xml में प्रत्येक फ़ीड के लिए आपके पास केवल एक समूह प्रविष्टि प्रविष्टि है। तो ग्रुपफिड होना चाहिए: [XmlElement (ElementName = "entry")] सार्वजनिक GroupEntry Entrie {get; सेट; प्रविष्टियों के बजाय}। –

+0

मैंने एक्सएमएल पोस्ट किया है। एक से अधिक प्रविष्टियां हो सकती हैं। – chobo2

संबंधित मुद्दे