Пример #1
0
        private static void LoadSimpleMemberFromXml(XElement mappedElement, SerializationContext context)
        {
            string value = null;

            if (mappedElement != null)
            {
                value = XmlOperations.ExtractValueAsStringFromXml(context.Mapping, mappedElement, context.NamespaceResolver);
            }

            var convertedValue = XmlMemberSerialization.GetMemberActualValue(context.DeserializeAs, value, context);

            context.Member.SetValue(context.Serializable, convertedValue);
        }
Пример #2
0
        private static void LoadArrayMemberFromXml(XElement mappedElement, SerializationContext context)
        {
            Array value = null;

            if (mappedElement != null)
            {
                XElement[] elements          = new XElement[0];
                var        arrayItemElements = mappedElement.Elements();
                if (arrayItemElements != null)
                {
                    elements = arrayItemElements.ToArray();
                }

                var arrayElementType = context.DeserializeAs.GetElementType();
                value = Array.CreateInstance(arrayElementType, elements.Length);
                for (int i = 0; i < elements.Length; i++)
                {
                    Type elementType = null;

                    // Short-circuit for nulls
                    var isNullAttribute = elements[i].Attribute(Constants.NullValueAttributeName);
                    if (isNullAttribute != null)
                    {
                        value.SetValue(null, i);
                        continue;
                    }

                    var typeAttribute = elements[i].Attribute(Constants.TypeInfoAttributeName);
                    if (typeAttribute != null)
                    {
                        elementType = Type.GetType(typeAttribute.Value);                         // TODO: handle all kind of exceptions
                    }
                    else
                    {
                        elementType = arrayElementType;
                    }

                    object convertedValue = null;
                    if (typeof(IXPathSerializable).IsAssignableFrom(elementType))
                    {
                        MethodInfo loadObjectFromXml = null;
                        try
                        {
                            loadObjectFromXml = typeof(XmlSerialization).GetMethod("LoadObjectFromXml", BindingFlags.Static | BindingFlags.NonPublic)
                                                .MakeGenericMethod(new Type[] { elementType });
                        }
                        catch (ArgumentException)
                        {
                            throw new SerializationException(
                                      String.Format(
                                          "Cannot deserialize {0} '{1}', type '{2}'. Type '{3}' does not have a default constructor.",
                                          context.Member.MemeberTypeString,
                                          context.Member.Name,
                                          context.Serializable.GetType(),
                                          elementType));
                        }
                        try
                        {
                            var itemDoc = new XDocument(elements[i]);
                            convertedValue = loadObjectFromXml.Invoke(
                                null,
                                new object[]
                            {
                                itemDoc,
                                context.Version,
                                context.FormatProvider,
                                context.NamespaceResolver
                            });
                        }
                        catch (TargetInvocationException ex)
                        {
                            throw new SerializationException(
                                      String.Format(
                                          "Cannot deserialize {0} '{1}', type '{2}'. Please refer to the inner exception for details.",
                                          context.Member.MemeberTypeString,
                                          context.Member.Name,
                                          context.Serializable.GetType()), ex);
                        }
                    }
                    else
                    {
                        convertedValue = XmlMemberSerialization.GetMemberActualValue(elementType, elements[i].Value, context);
                    }
                    value.SetValue(convertedValue, i);
                }
            }
            context.Member.SetValue(context.Serializable, value);
        }