0

I have this code:

[Serializable]
    [XmlRoot("ISO_CCY_CODES")]
    public class IsoCurrencyCodes
    {
        public IsoCurrencyCodes()
        {
            IsoCodes = new List<IsoCurrencyCode>();
        }

        public IsoCurrencyCodes(List<IsoCurrencyCode> isoCodes)
        {
            IsoCodes = isoCodes;
        }


        [XmlArrayItem("ISO_CURRENCY")]
        public List<IsoCurrencyCode> IsoCodes { get; set; }

        public static IEnumerable<IsoCurrencyCode> Get()
        {
            var doc = XDocument.Parse(XmlStringIsoCodes.XmlSource.Replace("\r\n", ""));
            var res = doc.Deserialize<IsoCurrencyCodes>();
            return res.IsoCodes;
        }
    }

    [Serializable]
    [XmlRoot("ISO_CURRENCY")]
    public class IsoCurrencyCode
    {
        public IsoCurrencyCode()
        {
        }

        [XmlElement(ElementName = "ENTITY")]
        public string Entity { get; set; }

        [XmlElement(ElementName = "CURRENCY")]
        public string Currency { get; set; }

        [XmlElement(ElementName = "ALPHABETIC_CODE")]
        public string Alpha_Code3 { get; set; }

        [XmlElement(ElementName = "NUMERIC_CODE")]
        public int NumCode { get; set; }

        [XmlElement(ElementName = "MINOR_UNIT")]
        public string MinorUnit { get; set; }
    }

And this code, for deserialization:

public static XDocument Serialize<T>(this T source)
        {
            var ser = new XmlSerializer(source.GetType());
            var sb = new StringBuilder();
            using (var writer = new StringWriter(sb))
            {
                ser.Serialize(writer, source);
            }
            return XDocument.Parse(sb.ToString());
        }

        public static T Deserialize<T>(this XDocument xmlDocument)
        {
            var xmlSerializer = new XmlSerializer(typeof (T));
            using (var reader = xmlDocument.CreateReader())
                return (T) xmlSerializer.Deserialize(reader);
        }

This is the XML source

But deserialization doesn't work. Please help. Thanks!

Nodir
  • 359
  • 1
  • 3
  • 17

1 Answers1

1

I believe you only want to use XMLArray if you have a collection element for all of the items to sit underneath. For example here it could be ISO_CURRENCIES. I'm assuming you can't change the source in this case, so just use this instead:

[XmlElement("ISO_CURRENCY")]
public List<IsoCurrencyCode> IsoCodes { get; set; }

You should find that works.

Additionally, if you find you have further problems in getting the deserialization classes right, you can have them autogenerated for you from the XML and then you can take a look at the code that is created:

xsd source.xml
xsd source.xsd /c

This will create source.cs which you can then use in your project or adapt for your own uses.

As a further note, you'll find you can't use int for Minor_Unit as it's nullable (look at ANTARCTICA). You can't deserialize straight to an int?, so you'll either have to make it a string or go via another property, look at this question for more information.

Community
  • 1
  • 1
Kasaku
  • 2,192
  • 16
  • 26