Deserialization to a list without a container element in XML

In all the examples that I saw when using the XmlSerializer anytime a list or array occurs, you have some kind of container element like this:

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

However, the XML I don't have does not have a container like Things above. He just starts repeating the elements. (By the way, XML is actually from the Google 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 the result, the type element is repeated without any type elements that the XmlSerializer seems to be expecting (or at least all the documents and examples I saw). The same goes for _address_component _.

The code that now looks like 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 try to deserialize XML, I get null elements in my Result _List _.

Can you suggest how I can make this work, since I currently do not see it?

+46
c # xml-serialization
Mar 11 2018-11-11T00:
source share
1 answer

Using

 [XmlElement("result")] public List<Result> Results { get; set; } 
+75
Mar 11 2018-11-11T00:
source share



All Articles