Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing into a List without a container element in XML

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?

like image 894
Colin Mackay Avatar asked Mar 11 '11 09:03

Colin Mackay


People also ask

What is Deserializing XML?

Deserialization is the process of reading an instance of an XML document and constructing an object that is strongly typed to the XML Schema (XSD) of the document. Before deserializing, an XmlSerializer must be constructed using the type of the object that is being deserialized.

Which class should be used to serialise an object in XML format?

XmlIgnoreAttribute Class (System.Xml.Serialization) Instructs the Serialize(TextWriter, Object) method of the XmlSerializer not to serialize the public field or public read/write property value.

Can I make XmlSerializer ignore the namespace on Deserialization?

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.

What is XmlSerializer C#?

The XmlSerializer enables you to control how objects are encoded into XML, it has a number of constructors. If you use any of the constructors other than the one that takes a type then a new temporary assembly is created EVERY TIME you create a serializer, rather than only once.


1 Answers

Use

[XmlElement("result")] public List<Result> Results { get; set; } 
like image 141
Aliostad Avatar answered Oct 27 '22 04:10

Aliostad