1

I have this snippet of XML and I'm attempting to Deserialize it. I have tried the following class to deserialize to but I dont get the address lines I only get the city state and postal code. Can someone point out my mistake? I can't see what I'm doing wrong. XML and class are just below.

XML:

         <RemitTo>
            <Address>
                <AddressLine lineNumber="1">Blah blah</AddressLine>
                <AddressLine lineNumber="2">bah bah bah</AddressLine>
                <AddressLine lineNumber="3">bah3</AddressLine>
                <City>Minneapolis</City>
                <State>MN</State>
                <PostalCode>55413</PostalCode>
                <Country isoCountryCode="US">United States</Country>
            </Address>
        </RemitTo>

CLASS:

[XmlRoot("RemitTo")]
    public partial class RemitTo
    {
        [XmlElementAttribute("Address")]
        public List<Address> RemitToAddress { get; set; }

    }

    public partial class Address
    {


        [XmlArray("Address")]
        [XmlArrayItem("AddressLine")]
        public List<string> AddressLine { get; set; }


        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
    }

In code im doing this.

RemitTo i;
XmlSerializer serializer = new XmlSerializer(typeof(RemitTo));
i = (RemitTo)serializer.Deserialize(addressReader);
Miguel
  • 2,019
  • 4
  • 29
  • 53

1 Answers1

3

It should be as simple as this

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

XmlArray isn't applicable, since we are already inside the Address class, and there is no further wrapper element around the child items.

Reference

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • StuartLC I must have spent 4 hours trying to figuered this out. Thank you for taking the time!. God i feel like such a dumb A**. – Miguel Dec 22 '14 at 18:20
  • 1
    More than welcome - I've spent many frustrating hours myself trying to shape xml into entities. When I get really stuck, I tend to use XSL to pre-project the xml into something closer to my entity graph. – StuartLC Dec 22 '14 at 18:22