Exemple #1
0
        private void ProcessSingleXmlElement(object obj, XmlMapping mapping, XElement elm)
        {
            var type  = mapping.PropertyCache.Property.PropertyType;
            var value = mapping.GetSingleXElementAttributeValue(elm);

            //priority is priority_raw on the root group, priority everywhere else. we need an alt xmlelement
            //ok we have one, we now need to treat attributevalue as a list

            try
            {
                ProcessXmlText(obj, mapping, value);
            }
            catch (Exception ex) when(!(ex is XmlDeserializationException))
            {
                throw new XmlDeserializationException(mapping.PropertyCache.Property.PropertyType, value?.ToString() ?? "null", ex);
            }
        }
Exemple #2
0
        private void ProcessXmlText(object obj, XmlMapping mapping, XElement elm)
        {
            var type = mapping.PropertyCache.Property.PropertyType;

            var str = elm != null ? $"\"{elm.Value}\"" : "null";

            Logger.Debug($"XElement contained {str}");

            elm = NullifyMissingValue(elm);

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

            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.PropertyCache.Property.Name}': cannot assign 'null' to value type '{type.Name}'."); //value types cant be null
            }
            mapping.PropertyCache.Property.SetValue(obj, finalValue);
        }
Exemple #3
0
        private void ProcessListEnumerableXmlElement(object obj, XmlMapping mapping, XElement elm)
        {
            var type           = mapping.PropertyCache.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();

            Logger.Debug($"XElement contained {elms.Count} element(s)");

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

            mapping.PropertyCache.Property.SetValue(obj, list);
        }
Exemple #4
0
        private void ProcessXmlElement(object obj, XmlMapping mapping, XElement elm)
        {
            var type = mapping.PropertyCache.Property.PropertyType;

            if (typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(string))
            {
                ProcessEnumerableXmlElement(obj, mapping, elm);
            }
            else
            {
                if (mapping.PropertyCache.Property.PropertyType.GetCustomAttribute <XmlRootAttribute>() != null)
                {
                    var elms = mapping.AttributeValue.Select(a => elm.Elements(a)).First(x => x != null).FirstOrDefault();

                    var result = elms != null?Deserialize(mapping.PropertyCache.Property.PropertyType, elms) : null;

                    mapping.PropertyCache.Property.SetValue(obj, result);
                }
                else
                {
                    ProcessSingleXmlElement(obj, mapping, elm);
                }
            }
        }