5

I am trying to serialize an XML into a Class, PostalAddress. The Postal Address looks like this

 public class PostalAddress
{
    public string DeliverTo { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }

    [XmlAttribute("isoCountryCode")]
    public string CountryCode { get; set; }

    public string Country { get; set; }
}

The input XML document is

<PostalAddress>
   <DeliverTo>SomeBody</DeliverTo>
   <Street>Some Street Address</Street>
   <City>MyCity</City>
   <PostalCode>US-1013</PostalCode>
   <Country isoCountryCode="US">US</Country>
</PostalAddress>

The C# code I am using for Serializing is

Now After Deserialization, I am able to see the values from XML in the corresponding properties but It is Missing the Attribute Value which I wanted to map into a Property, CountryCode.

var serializer = new XmlSerializer(typeof(PostalAddress));
var xmlReaderSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Parse}
PostalAddress postalAddress;
using (var reader = XmlReader.Create(new StringReader(inputDocument.ToString()), xmlReaderSettings))
{
     postalAddress= (PostalAddress)serializer.Deserialize(reader);
}

Can you please help where am I missing things here?``

3 Answers 3

2

XmlAttribute will not work in the context you're using it, because it's looking at the element PostalAddress, which has no attributes. The attribute you want, isoCountryCode, is on the sub element Country.

To make this work you need to define another class for the Country element. For example, you could use classes like the below.

public class PostalAddress
{
    public string DeliverTo { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }    
    public Country Country { get; set; }
}

[XmlRoot("Country")]
public class Country 
{
    [XmlAttribute("isoCountryCode")]
    public string IsoCountryCode { get; set;}
    [XmlText]
    public string Name { get; set; }
}

Alternatively, if you can't change your class structure, you can look at custom deserialisation with the IXmlSerializable interface.

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

1 Comment

Thank you very much for the response. I got it working now :)
1

You have to create class for Country element with two property named CountryCode & Text...

public class PostalAddress
{
    public string DeliverTo { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }

    public Country Country { get; set; }

}

public class Country
{
    [XmlAttribute("isoCountryCode")]
    public string CountryCode { get; set; }
    [XmlText]
    public string Description { get; set; }
}

1 Comment

Thank you very much for the response. I got it working now :)
0

When using the XmlAttribute-attribute, the attribute should be located on the element that corresponds to the class. In other words, if you would structure your XML document like this, the attribute would be de-serialized into the property:

<PostalAddress isoCountryCode="US">...</PostalAddress>

However, of you add another class for the country, you can deserialize the XML properly:

public class PostalAddress
{
    public string DeliverTo { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }

    [XmlElement("Country")]
    public CountryCode CountryCode;
}

public class CountryCode
{
    [XmlAttribute("isoCountryCode")]
    public string Code { get; set; }
    [XmlText]
    public string Country { get; set; }
}

1 Comment

Thank you very much for the response. I got it working now :)

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.