0

I'm trying to convert the following XML to the object,

<Addresses><Address><AddressLine1>61A PEEL ST</AddressLine1><AddressLine2></AddressLine2><Suburb>WEST MELBOURNE</Suburb><State>VIC</State><PostCode>3003</PostCode></Address><Address><AddressLine1>U 101/61 PEEL ST</AddressLine1><AddressLine2></AddressLine2><Suburb>WEST MELBOURNE</Suburb><State>VIC</State><PostCode>3003</PostCode></Address><Address><AddressLine1>U 102/61 PEEL ST</AddressLine1><AddressLine2></AddressLine2><Suburb>WEST MELBOURNE</Suburb><State>VIC</State><PostCode>3003</PostCode></Address><Address><AddressLine1>59 PEEL ST</AddressLine1><AddressLine2></AddressLine2><Suburb>WEST MELBOURNE</Suburb><State>VIC</State><PostCode>3003</PostCode></Address><Address><AddressLine1>63 PEEL ST</AddressLine1><AddressLine2></AddressLine2><Suburb>WEST MELBOURNE</Suburb><State>VIC</State><PostCode>3003</PostCode></Address><Address><AddressLine1>63A PEEL ST</AddressLine1><AddressLine2></AddressLine2><Suburb>WEST MELBOURNE</Suburb><State>VIC</State><PostCode>3003</PostCode></Address><Address><AddressLine1>57 PEEL ST</AddressLine1><AddressLine2></AddressLine2><Suburb>WEST MELBOURNE</Suburb><State>VIC</State><PostCode>3003</PostCode></Address><Address><AddressLine1>57A PEEL ST</AddressLine1><AddressLine2></AddressLine2><Suburb>WEST MELBOURNE</Suburb><State>VIC</State><PostCode>3003</PostCode></Address></Addresses>

Using the below code to deserialise the XML,

XmlSerializer serializer = new XmlSerializer(typeof(Addresses));
            using (TextReader reader = new StringReader(addressValidationResult.Alternates))
            {
                Addresses result = (Addresses)serializer.Deserialize(reader);
            }

With "addressValidationResult.Alternates" provides the above XML string as a result.

PFB my model,

namespace Models
{
    public class Addresses
    {
         public List<Address> AddressesList { get; set; }
    }

    public class Address{
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string Suburb { get; set; }
        public string State { get; set; }
        public string PostCode { get; set; }
    }
}

Result is coming with an empty list without any data. The code is not throwing an exception but the data is also not getting converted.

Can one please let me know what I'm missing here

Viggo Lundén
  • 748
  • 1
  • 6
  • 31
Aryan M
  • 571
  • 3
  • 12
  • 33

1 Answers1

0

Your models and code for deserialization looks correct, the only thing that was missing is some XmlElement attributes. Adding [XmlElement("Address")] to the AddressesList property is the key solution here.

    [XmlRoot("Addresses")]
    public class Addresses
    {
        [XmlElement("Address")]
        public List<Address> AddressesList { get; set; }
    }

    public class Address
    {
        [XmlElement("AddressLine1")]
        public string AddressLine1 { get; set; }
        [XmlElement("AddressLine2")]
        public string AddressLine2 { get; set; }
        [XmlElement("Suburb")]
        public string Suburb { get; set; }
        [XmlElement("State")]
        public string State { get; set; }
        [XmlElement("PostCode")]
        public string PostCode { get; set; }
    }