Miguel
Miguel

Reputation: 2079

Deserializing XML using C#

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);

Upvotes: 1

Views: 65

Answers (1)

StuartLC
StuartLC

Reputation: 107357

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

Upvotes: 3

Related Questions