2011-03-11 18 views
47

सभी उदाहरणों में मैं XmlSerializer किसी भी समय किसी सूची या सरणी होता है आप इस तरह कंटेनर तत्व न किसी तरह का उपयोग करने का देखा है बिना एक सूची में deserializing:XML में एक कंटेनर तत्व

<MyXml> 
    <Things> 
    <Thing>One</Thing> 
    <Thing>Two</Thing> 
    <Thing>Three</Thing> 
    </Things> 
</MyXml> 

हालांकि, एक्सएमएल में मेरे पास चीजें जैसी कोई कंटेनर नहीं है। यह सिर्फ तत्वों को दोहराना शुरू करता है। (संयोग से, एक्सएमएल गूगल के जियोकोड एपीआई से वास्तव में है)

तो, मैं एक्सएमएल है कि इस तरह दिखता है:

<?xml version="1.0" encoding="UTF-8"?> 
<GeocodeResponse> 
    <status>OK</status> 
    <result> 
    <type>locality</type> 
    <type>political</type> 
    <formatted_address>Glasgow, City of Glasgow, UK</formatted_address> 
    <address_component> 
     <long_name>Glasgow</long_name> 
     <short_name>Glasgow</short_name> 
     <type>locality</type> 
     <type>political</type> 
    </address_component> 
    <address_component> 
     <long_name>East Dunbartonshire</long_name> 
     <short_name>East Dunbartonshire</short_name> 
     <type>administrative_area_level_3</type> 
     <type>political</type> 
    </address_component> 
    <!-- etc... --> 
    </result> 
    <result> 
    <!-- etc... --> 
    </result> 
    <result> 
    <!-- etc... --> 
    </result> 
</GeocodeResponse> 

आप अंदर परिणामप्रकार तत्व किसी भी बिना दोहराता देख सकते हैं तत्व XmlSerializer अपेक्षा करता है (या कम से कम सभी दस्तावेज और उदाहरण मैंने देखा है)। _address_component_ के लिए भी यही है।

कोड मैं वर्तमान में है इस तरह दिखता है:

[XmlRoot("GeocodeResponse")] 
public class GeocodeResponse 
{ 
    public GeocodeResponse() 
    { 
     this.Results = new List<Result>(); 
    } 

    [XmlElement("status")] 
    public string Status { get; set; } 

    [XmlArray("result")] 
    [XmlArrayItem("result", typeof(Result))] 
    public List<Result> Results { get; set; } 
} 

हर बार जब मैं deserialize एक्सएमएल मैं अपने परिणाम _List_ में शून्य आइटम प्राप्त करने का प्रयास।

क्या आप सुझाव दे सकते हैं कि मैं इसे कैसे काम कर सकता हूं क्योंकि मैं वर्तमान में इसे नहीं देख रहा हूं?

+1

FYI करें: इस सटीक सवाल का एसओ तैनात किया गया था, कल। http://stackoverflow.com/questions/5259911/deserialize-multiple-xml-elements-with-the-same-name-through-xmlserializer-class xml-serialization के तहत, उस और उसके बीच एक हस्तक्षेप क्यू है टैग। साथ ही, यह पहली बार नहीं पूछा गया/उत्तर दिया गया है। – Cheeso

+1

अच्छा, मुझे यह नहीं मिला! मैंने पहले खोज की, और जब मैंने अपने प्रश्न शीर्षक में रखा तो सभी सुझावों के माध्यम से क्लिक किया। –

उत्तर

78

उपयोग

[XmlElement("result")] 
public List<Result> Results { get; set; } 
+2

यह काम करता है - मुझे विश्वास नहीं है कि यह बेवकूफ सरल था कि मैंने इसे याद किया। रवींद्र! –

+0

कोई जांच नहीं, – Aliostad

+1

मदद करने में प्रसन्नता आप महोदय, एक विद्वान और सज्जन हैं। – user942620

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