Exemple #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);
        }
Exemple #2
0
        private static void SerializeSimpleMember(
            XDocument document,
            object value,
            SerializationContext context)
        {
            string valueString = XmlMemberSerialization.GetMemberXmlValue(value, context);

            XmlOperations.PutValueConvertedToStringToXml(
                document,
                valueString,
                context.DeserializeAs,
                context.Mapping,
                false,
                context.EmitTypeInfo,
                context.NamespaceResolver);
        }
Exemple #3
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);
        }
Exemple #4
0
        private static void SerializeArrayMember(
            XDocument document,
            object array,
            SerializationContext context,
            HashSet <object> objectsAlreadySerialized)
        {
            Debug.Assert(array != null);

            var mappedElement = XmlOperations.GetOrCreateElement(document, context.Mapping.ElementXPath, context.NamespaceResolver);

            // Emit type info if necessary
            if (context.EmitTypeInfo)
            {
                if (mappedElement.Attribute(Constants.TypeInfoAttributeName) != null)
                {
                    throw new SerializationException(
                              String.Format(
                                  "Cannot serialize object with type information. Mapping XPath '{0}' is used by more than 1 value.",
                                  context.Mapping.ElementXPath));
                }
                mappedElement.Add(new XAttribute(Constants.TypeInfoAttributeName, context.DeserializeAs.AssemblyQualifiedName.ToString()));
            }

            foreach (var item in (IEnumerable)array)
            {
                var serializableItem = item as IXPathSerializable;
                if (serializableItem != null)
                {
                    var objectElement = ToXmlInternal(
                        new XDocument(),
                        serializableItem,
                        objectsAlreadySerialized,
                        useMembers: null,
                        parentMapping: null,
                        emitTypeInfo: context.EmitTypeInfo,
                        version: context.Version,
                        provider: context.FormatProvider,
                        resolver: context.NamespaceResolver).Root;                         // TODO: pass along every argument from outer ToXml
                    if (context.EmitTypeInfo)
                    {
                        objectElement.Add(new XAttribute(Constants.TypeInfoAttributeName, serializableItem.GetType().AssemblyQualifiedName.ToString()));
                    }
                    mappedElement.Add(objectElement);
                }
                else
                {
                    string valueString       = XmlMemberSerialization.GetMemberXmlValue(item, context);
                    Type   deserializeItemAs = null;
                    if (item != null && context.EmitTypeInfo)
                    {
                        deserializeItemAs = item.GetType();
                    }
                    XmlOperations.PutValueConvertedToStringToXml(
                        document,
                        valueString,
                        deserializeItemAs,
                        context.Mapping,
                        true,
                        context.EmitTypeInfo,
                        context.NamespaceResolver);
                }
            }
        }