1

I get a response which contains following:

<carList>
<numberOfRecords>237</numberOfRecords>
<year>1982</year>
<year>1983</year>
<year>1978</year>
<year>1955</year>
<year>1998</year>
</carList>

this is part of bigger xml doc, I can desterilizes the rest but this part I cannot. rest of my desterilized objects comes fine. here is my models:

public class Response 
{
   [XmlArray( "carList")]
   [XmlArrayItem("year")]
   //[XmlElement("carList")]
   public CarList? CarList { get; set; }
}
    public class CarList
    {
        [XmlElement("numberOfRecords")]
        public int? NumberOfRecords { get; set; }

        [XmlElement("year")]
        public List<string>? Year { get; set; }
    }

my deserializer:

XmlDeserializer xmldes = new XmlDeserializer();

var deserializedResponse = xmldes.Deserialize<Response>(response);

1 Answer 1

1

You only have a single carList, so this should be:

[XmlElement("carList")]
public CarList? CarList { get; set; }

assuming this is a child node, and not the root element (in which case: [XmlRoot("carList")] on CarList itself, and lose Response completely)

Sign up to request clarification or add additional context in comments.

8 Comments

you're right, it's not a root element. it's part of bigger xml. my carList contains only a number of cars along with list of year. if i use the way you mentioned, 'NumberofRecords" gets populated but List of Year is empty
@Pamador works fine here: gist.github.com/mgravell/0a1d3a1de0cd9ab0614b375dc5425bb8 - do you have a repro that shows it not working? is the real code perhaps using xml namespaces and/or namespace prefixes?
thanks for the comment. there is name space on root element if that was you're referring to. i did similar thing but it didn't work. when i deserialise the xml, number of record gets the correct value, but my list has none.
@Pamador namespaces are critically important in xml deserialization; if you want code that matches your data, you'll need to show example xml that includes something like your scenario - otherwise it won't work
yes, i tried that in different project same as you did, and it works over there. here is the xml: <ABRPayloadSearchResults xmlns:xsd="w3.org/2001/XMLSchema" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns="abr.business.gov.au/ABRXMLSearch"> <response> <abnList> <numberOfRecords>2680</numberOfRecords> <abn>95437472757</abn> <abn>38023383928</abn> <abn>12839919648</abn> <abn>50165055391</abn> <abn>60306167177</abn> </abnList> </response> </ABRPayloadSearchResults>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.