Example #1
0
        private void ProcessListEnumerableXmlElement(object obj, XmlMapping mapping, XElement elm)
        {
            var type           = mapping.Property.PropertyType;
            var underlyingType = type.GetGenericArguments().First();

            var list = Activator.CreateInstance(type);

            var elms = mapping.AttributeValue.Select(a => elm.Elements(a)).First(x => x != null).ToList();

            foreach (var e in elms)
            {
                ((IList)list).Add(Deserialize(underlyingType, e));
            }

            mapping.Property.SetValue(obj, list);
        }
Example #2
0
        private void ProcessSingleXmlElement(object obj, XmlMapping mapping, XElement elm)
        {
            var type  = mapping.Property.PropertyType;
            var value = mapping.GetSingleXElementAttributeValue(elm);

            try
            {
                ProcessXmlText(obj, mapping, value);
                //value = NullifyMissingValue(value);

                //var finalValue = value == null ? null : GetValue(type, value.Value);

                //if (type.IsValueType && Nullable.GetUnderlyingType(type) == null && finalValue == null)
                //    throw new XmlDeserializationException($"An error occurred while attempting to deserialize XML element '{mapping.AttributeValue.First()}' to property '{mapping.Property.Name}': cannot assign 'null' to value type '{type.Name}'."); //value types cant be null

                //mapping.Property.SetValue(obj, finalValue);
            }
            catch (Exception ex) when(!(ex is XmlDeserializationException))
            {
                throw new XmlDeserializationException(mapping.Property.PropertyType, value?.ToString() ?? "null", ex);
            }
        }