64

In all the examples I've seen of using XmlSerializer any time a list or array happens you have some sort of container element like this:

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

However, the XML I have has no container similar to Things above. It just starts repeating elements. (Incidentally, the XML is actually from Google's Geocode API)

So, I have XML that looks like this:

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

As you can see inside result the type element repeats without any types element that XmlSerializer appears to expect (or at least all the documents and examples I've seen). The same goes for the address_component.

The code I currently have looks something like this:

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

Every time I attempt to deserialize the XML I get zero items in my Result List.

Can you suggest how I may get this to work as I'm currently not seeing it?

riQQ
  • 9,878
  • 7
  • 49
  • 66
Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
  • 1
    FYI: this exact question was posted to SO, yesterday. http://stackoverflow.com/questions/5259911/deserialize-multiple-xml-elements-with-the-same-name-through-xmlserializer-class There is one intervening Q between that one and this one, under the xml-serialization tag. Also, it's not the first time it's been asked/answered. – Cheeso Mar 11 '11 at 14:44
  • 2
    Well, I couldn't find it! I searched first, and clicked through all the the suggestions when I put in my question title. – Colin Mackay Mar 14 '11 at 09:14

1 Answers1

104

Use

[XmlElement("result")]
public List<Result> Results { get; set; }
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • 3
    It worked - I cannot believe it was that stupidly simple that I missed it. Doh! – Colin Mackay Mar 11 '11 at 10:05
  • I have attempted to use this, and for some reason it is only grabbing the first in the list of items. I'm not sure why, and it's getting really aggravating. – Grungondola Oct 03 '19 at 19:35
  • We are facing the same situation of repeating XML elements without container, however we are using the DataContract serializer (a WCF service) and, have not found a solution for deserializing the repeating XML elements without container into a list; the list property ends up with zero elements. Is there a similar solution to deserialize repeating XML elements without container in this case? [DataMember(IsRequired = false, Order = 3)] public List Identifications { get; set; } – Only You Jul 11 '22 at 21:30