Exemple #1
0
        private object ToObject(object instance, XmlNode xmlNode, string nodeName = null)
        {
            Type                type                = instance.GetType();
            ClassMetaData       classMetaData       = MetaDataCache.Get(type);
            XmlMappingOperation xmlMappingOperation = xmlMapperAttribute.MappingOperation;

            if (classMetaData.ClassAttributeContext.ContainsAttribute <XmlMapperAttribute>())
            {
                XmlMapperAttribute xmlMapper = classMetaData.ClassAttributeContext.GetAttribute <XmlMapperAttribute>();
                xmlMappingOperation = xmlMapper.MappingOperation;
                if (nodeName == null)
                {
                    nodeName = xmlMapper.ParentNodeName;
                    xmlNode  = xmlNode.SelectSingleNode($"//{nodeName}");
                }
            }

            foreach (IFieldPropertyInfo property in classMetaData.Properties)
            {
                string      propertyName = property.Name;
                System.Type propertyType = property.Type;

                if (classMetaData.HasPropertyAttributeContext <PropertyAttributeContext>(property))
                {
                    PropertyAttributeContext propertyAttributeContext = classMetaData.GetAttributeContextForProperty <PropertyAttributeContext>(property);

                    if (propertyAttributeContext.HasIgnoreAttribute)
                    {
                        continue;
                    }

                    propertyName = propertyAttributeContext.HasNameAttribute ? propertyAttributeContext.NameAttribute.Name : propertyName;
                }

                object propertyValue = GetNodeValue(xmlMappingOperation, xmlNode, nodeName, propertyName);

                if (classMetaData.HasPropertyAttributeContext <XmlPropertyAttributeContext>(property))
                {
                    XmlPropertyAttributeContext xmlPropertyAttributeContext = classMetaData.GetAttributeContextForProperty <XmlPropertyAttributeContext>(property);

                    if (xmlPropertyAttributeContext.HasXmlListAttribute)
                    {
                        XmlListAttribute xmlList = xmlPropertyAttributeContext.XmlListAttribute;
                        XmlNodeList      results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/{xmlList.NodeName}");
                        if (results.Count > 0)
                        {
                            object childInstance = CollectionXmlNodeListToObject(results, propertyType);
                            property.SetValue(instance, childInstance);
                        }
                        continue;
                    }
                    else if (xmlPropertyAttributeContext.HasXmlDictionaryAttribute)
                    {
                        XmlDictionaryAttribute xmlList = xmlPropertyAttributeContext.XmlDictionaryAttribute;
                        XmlNodeList            results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/*");
                        if (results.Count > 0)
                        {
                            object childInstance = DictionaryXmlNodeListToObject(results, propertyType);
                            property.SetValue(instance, childInstance);
                        }
                        continue;
                    }
                    else if (xmlPropertyAttributeContext.HasXmlFlattenHierarchyAttribute)
                    {
                        object childInstance = Activator.CreateInstance(propertyType);
                        childInstance = ToObject(childInstance, xmlNode, nodeName);
                        property.SetValue(instance, childInstance);
                        continue;
                    }
                    else if (xmlPropertyAttributeContext.HasXmlPropertyConverterAttribute && propertyValue != null)
                    {
                        XmlPropertyConverterAttribute converter = xmlPropertyAttributeContext.XmlPropertyConverterAttribute;
                        try
                        {
                            propertyValue = converter.ConvertToSourceType(propertyValue);
                        }
                        catch (Exception ex)
                        {
                            throw new XmlPropertyConverterException("XmlPropertyConverter threw an exception", ex);
                        }
                    }
                }
                else
                {
                    if (propertyType.IsDictionary())
                    {
                        XmlNodeList results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/*");
                        if (results.Count > 0)
                        {
                            object childInstance = DictionaryXmlNodeListToObject(results, propertyType);
                            property.SetValue(instance, childInstance);
                        }
                        continue;
                    }
                    else if (propertyType.IsCollection())
                    {
                        string      listItemNodeName = propertyType.GenericTypeArguments[0].Name;
                        XmlNodeList results          = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/{listItemNodeName}");
                        if (results.Count > 0)
                        {
                            object childInstance = CollectionXmlNodeListToObject(results, propertyType);
                            property.SetValue(instance, childInstance);
                        }
                        continue;
                    }
                    if (propertyType.IsClass && MetaDataCache.Contains(propertyType))
                    {
                        // TODO: Dont think this will work
                        object  childInstance = Activator.CreateInstance(propertyType);
                        XmlNode results       = xmlNode.SelectSingleNode($"//{nodeName}/{propertyName}");
                        if (results != null)
                        {
                            childInstance = ToObject(childInstance, results, propertyName);
                            property.SetValue(instance, childInstance);
                        }
                        continue;
                    }
                }

                if (propertyValue == null)
                {
                    continue;
                }

                property.SetValue(instance, UniversalTypeConverter.Convert(propertyValue, propertyType));
            }

            return(instance);
        }
Exemple #2
0
        private XElement ToXml(object instance, Type type, XElement element = null, string nodeName = null)
        {
            ClassMetaData       classMetaData       = MetaDataCache.Get(type);
            XmlMappingOperation xmlMappingOperation = xmlMapperAttribute.MappingOperation;
            bool ignoreNulls = xmlMapperAttribute.IgnoreNulls;

            if (classMetaData.ClassAttributeContext.ContainsAttribute <XmlMapperAttribute>())
            {
                XmlMapperAttribute xmlMapper = classMetaData.ClassAttributeContext.GetAttribute <XmlMapperAttribute>();
                xmlMappingOperation = xmlMapper.MappingOperation;
                ignoreNulls         = xmlMapper.IgnoreNulls;

                if (element == null)
                {
                    element  = new XElement(xmlMapper.ParentNodeName);
                    nodeName = xmlMapper.ParentNodeName;
                }
            }

            //element.Name = nodeName;

            foreach (IFieldPropertyInfo property in classMetaData.Properties)
            {
                string propertyName  = property.Name;
                object propertyValue = property.GetValue(instance);
                Type   propertyType  = property.Type;

                if (propertyValue == null && ignoreNulls)
                {
                    continue;
                }

                if (classMetaData.HasPropertyAttributeContext <PropertyAttributeContext>(property))
                {
                    PropertyAttributeContext propertyAttributeContext = classMetaData.GetAttributeContextForProperty <PropertyAttributeContext>(property);
                    if (propertyAttributeContext.HasIgnoreAttribute)
                    {
                        continue;
                    }

                    propertyName = propertyAttributeContext.HasNameAttribute ? propertyAttributeContext.NameAttribute.Name : propertyName;
                }

                if (classMetaData.HasPropertyAttributeContext <XmlPropertyAttributeContext>(property))
                {
                    XmlPropertyAttributeContext xmlPropertyAttributeContext = classMetaData.GetAttributeContextForProperty <XmlPropertyAttributeContext>(property);

                    if (xmlPropertyAttributeContext.HasXmlPropertyConverterAttribute && propertyValue != null)
                    {
                        XmlPropertyConverterAttribute converter = xmlPropertyAttributeContext.XmlPropertyConverterAttribute;
                        try
                        {
                            propertyValue = converter.ConvertToDestinationType(propertyValue);
                            AddToXElement(element, xmlMappingOperation, propertyName, propertyValue);
                            continue;
                        }
                        catch (Exception ex)
                        {
                            throw new XmlPropertyConverterException("XmlPropertyConverter threw an exception", ex);
                        }
                    }
                    else if (xmlPropertyAttributeContext.HasXmlDictionaryAttribute)
                    {
                        XmlDictionaryAttribute xmlDictionary = xmlPropertyAttributeContext.XmlDictionaryAttribute;
                        element.Add(DictionaryToXElement(propertyValue, propertyName));
                        continue;
                    }
                    else if (xmlPropertyAttributeContext.HasXmlListAttribute)
                    {
                        XmlListAttribute xmlList = xmlPropertyAttributeContext.XmlListAttribute;
                        element.Add(CollectionToXElement(propertyValue, propertyName, xmlList.NodeName));
                        continue;
                    }
                    else if (xmlPropertyAttributeContext.HasXmlFlattenHierarchyAttribute)
                    {
                        element = ToXml(propertyValue, propertyType, element, propertyName);
                        continue;
                    }
                }
                else
                {
                    if (propertyType.IsDictionary())
                    {
                        element.Add(DictionaryToXElement(propertyValue, propertyName));
                        continue;
                    }
                    else if (propertyType.IsCollection())
                    {
                        element.Add(CollectionToXElement(propertyValue, propertyName, propertyType.GenericTypeArguments[0].Name));
                        continue;
                    }
                    else if (propertyType.IsClass && MetaDataCache.Contains(propertyType))
                    {
                        XElement propertyElement = new XElement(propertyName);
                        propertyElement = ToXml(propertyValue, propertyType, propertyElement, propertyName);
                        element.Add(propertyElement);
                        continue;
                    }
                }

                AddToXElement(element, xmlMappingOperation, propertyName, propertyValue);
            }

            return(element);
        }