Exemple #1
0
        private static IXPathSerializable LoadFromXmlInternal(
            XDocument document,
            IXPathSerializable serializable,
            MemberInfo[] useMembers,
            Mapping parentMapping,
            int version,
            IFormatProvider provider,
            XmlNamespaceManager resolver)
        {
            // Initialize namespace resolver from class attributes
            if (resolver == null)
            {
                resolver = new XmlNamespaceManager(new NameTable());
            }
            var namespaceAttributes = serializable.GetType().GetCustomAttributes(typeof(NamespacePrefixAttribute), true);

            foreach (NamespacePrefixAttribute namespaceAttribute in namespaceAttributes)
            {
                resolver.AddNamespace(namespaceAttribute.Prefix, namespaceAttribute.Uri);
            }

            var typeInfo = SerializableTypeInfo.GetTypeInfo(serializable.GetType());

            var membersToLoad = typeInfo.Members.Where(x => useMembers == null || useMembers.Length == 0 || useMembers.Contains(x.MemberInfo));

            LoadMembersFromXml(
                document,
                serializable,
                membersToLoad.ToArray(),
                useMembers,
                parentMapping,
                version,
                provider,
                resolver);

            return(serializable);
        }
Exemple #2
0
        private static XDocument ToXmlInternal(
            XDocument document,
            IXPathSerializable serializable,
            HashSet <object> objectsAlreadySerialized,
            MemberInfo[] useMembers,
            Mapping parentMapping,
            bool emitTypeInfo,
            int version,
            IFormatProvider provider,
            XmlNamespaceManager resolver)
        {
            // Take care of circular references
            if (objectsAlreadySerialized.Contains(serializable))
            {
                throw new SerializationException(
                          String.Format("A circular reference was detected while serializing an object of type '{0}'", serializable.GetType()));
            }
            else
            {
                objectsAlreadySerialized.Add(serializable);
            }

            if (document == null)
            {
                document = new XDocument();
            }

            // Initialize namespace resolver from class attributes
            if (resolver == null)
            {
                resolver = new XmlNamespaceManager(new NameTable());
            }
            var namespaceAttributes = serializable.GetType().GetCustomAttributes(typeof(NamespacePrefixAttribute), true);

            foreach (NamespacePrefixAttribute namespaceAttribute in namespaceAttributes)
            {
                resolver.AddNamespace(namespaceAttribute.Prefix, namespaceAttribute.Uri);
            }

            // Ensure minimal xml structure
            var minimalXmlAttributes = serializable.GetType().GetCustomAttributes(typeof(MinimalXmlStructureAttribute), true);

            if (minimalXmlAttributes != null)
            {
                foreach (MinimalXmlStructureAttribute minimalXmlAttribute in minimalXmlAttributes)
                {
                    string xPath = minimalXmlAttribute.ElementXPath;
                    if (String.IsNullOrWhiteSpace(xPath))
                    {
                        xPath = XmlConvert.EncodeName(serializable.GetType().Name);
                    }
                    XmlOperations.GetOrCreateElement(document, xPath, resolver);
                }
            }

            var typeInfo = SerializableTypeInfo.GetTypeInfo(serializable.GetType());

            var membersToSerialize = typeInfo.Members.Where(x => useMembers == null || useMembers.Length == 0 || useMembers.Contains(x.MemberInfo));

            SerializeMembers(
                document,
                serializable,
                objectsAlreadySerialized,
                membersToSerialize.ToArray(),
                useMembers,
                parentMapping,
                emitTypeInfo,
                version,
                provider,
                resolver);

            // Return resulting document
            return(document);
        }