Beispiel #1
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);
        }
Beispiel #2
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);
                }
            }
        }