예제 #1
0
        private object ReadComponent(XPathResult result, bool ifExists, IDictionaryAdapter dictionaryAdapter)
        {
            XPathNavigator source;

            if (result.GetNavigator(false, true, out source) == false || (source == null && ifExists))
            {
                return(null);
            }

            XPathAdapter xpathAdapter;
            var          elementType = result.Type;

            if (source != null)
            {
                if (result.XmlMeta != null)
                {
                    elementType = result.XmlMeta.Type;
                }
                else
                {
                    var xmlType = Context.GetXmlType(source);
                    elementType = dictionaryAdapter.GetXmlSubclass(xmlType, elementType) ?? elementType;
                }
                xpathAdapter = new XPathAdapter(source, this);
            }
            else
            {
                Func <XPathNavigator> createSource = () => result.GetNavigator(true);
                xpathAdapter = new XPathAdapter(createSource, this);
            }

            return(Create(dictionaryAdapter, elementType, null, xpathAdapter));
        }
예제 #2
0
        private object ReadSimple(XPathResult result)
        {
            var node = result.GetNavigator(false);

            if (node != null)
            {
                if (result.Type.IsEnum)
                {
                    return(Enum.Parse(result.Type, node.Value));
                }
                else if (result.Type == typeof(Guid))
                {
                    return(new Guid(node.Value));
                }

                try
                {
                    return(node.ValueAs(result.Type));
                }
                catch (InvalidCastException)
                {
                    object value;
                    if (DefaultXmlSerializer.Instance.ReadObject(result, node, out value))
                    {
                        return(value);
                    }
                }
            }
            if (result.Result != null)
            {
                return(Convert.ChangeType(result.Result, result.Type));
            }

            return(null);
        }
예제 #3
0
        private void WriteSimple(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
        {
            if (value == null)
            {
                result.Remove();
                if (result.Property != null)
                {
                    dictionaryAdapter.This.ExtendedProperties.Remove(result.Property.PropertyName);
                }
                return;
            }

            var node = result.GetNavigator(true);

            if (result.Type.IsEnum || result.Type == typeof(Guid))
            {
                node.SetTypedValue(value.ToString());
            }
            else
            {
                try
                {
                    node.SetTypedValue(value);
                }
                catch (InvalidCastException)
                {
                    if (DefaultXmlSerializer.Instance.WriteObject(result, node, value) && result.Property != null)
                    {
                        dictionaryAdapter.This.ExtendedProperties[result.Property.PropertyName] = value;
                    }
                }
            }
        }
예제 #4
0
        private void WriteCollection(XPathResult result, ref object value, IDictionaryAdapter dictionaryAdapter)
        {
            if (value is byte[])
            {
                var node = result.GetNavigator(true);
                node.SetValue(Convert.ToBase64String((byte[])value));
                return;
            }

            result.Remove(value == null);

            if (value != null)
            {
                if (result.Type.IsArray)
                {
                    WriteArray(result, value, dictionaryAdapter);
                }
                else if (result.Type.IsGenericType)
                {
                    WriteList(result, value, dictionaryAdapter);
                }
                if (result.Property != null)
                {
                    value = ((IDictionaryPropertyGetter)this).GetPropertyValue(dictionaryAdapter, result.Key, null, result.Property, false);
                }
            }
        }
예제 #5
0
        private void WriteComponent(XPathResult result, ref object value, IDictionaryAdapter dictionaryAdapter)
        {
            var source = value as IDictionaryAdapter;

            if (source != null)
            {
                var sourceAdapter = For(source);
                if (sourceAdapter != null)
                {
                    var sourceRoot = sourceAdapter.Root;
                    var resultNode = result.GetNavigator(false);
                    if (sourceRoot != null && resultNode != null && sourceRoot.IsSamePosition(resultNode))
                    {
                        return;
                    }
                }

                var node = result.RemoveChildren();
                if (result.Type != source.Meta.Type && result.OmitPolymorphism == false)
                {
                    var xmlType = source.GetXmlMeta().XmlType;
                    var context = GetEffectiveContext(dictionaryAdapter);
                    context.SetXmlType(xmlType.TypeName, xmlType.Namespace, node);
                }

                var element = (IDictionaryAdapter)ReadComponent(result, false, dictionaryAdapter);
                source.CopyTo(element);
                value = element;
            }
        }
예제 #6
0
        private object ReadFragment(XPathResult result)
        {
            var node = result.GetNavigator(false);

            if (result.Type == typeof(XmlElement))
            {
                var document = new XmlDocument();
                document.Load(node.ReadSubtree());
                return(document.DocumentElement);
            }
            return(node.Clone());
        }
예제 #7
0
        private object ReadComponent(XPathResult result, bool ifExists, IDictionaryAdapter dictionaryAdapter)
        {
            XPathAdapter xpathAdapter;
            var          source = result.GetNavigator(false);

            if (source == null && ifExists)
            {
                return(null);
            }

            var elementType = result.Type;

            if (source != null)
            {
                if (result.XmlMeta != null)
                {
                    elementType = result.XmlMeta.Type;
                }
                else
                {
                    var xmlType = Context.GetXmlType(source);
                    elementType = dictionaryAdapter.GetXmlSubclass(xmlType, elementType) ?? elementType;
                }
                xpathAdapter = new XPathAdapter(source, this);
            }
            else
            {
                Func <XPathNavigator> createSource = () => result.GetNavigator(true);
                xpathAdapter = new XPathAdapter(createSource, this);
            }

            var component = Create(dictionaryAdapter, elementType, null, xpathAdapter);

            if (result.Property != null)
            {
                dictionaryAdapter.This.ExtendedProperties[result.Property.PropertyName] = component;
            }

            return(component);
        }
예제 #8
0
        private void WriteFragment(XPathResult result, IXPathNavigable value)
        {
            var node = result.GetNavigator(true);

            if (node == null)
            {
                root.AppendChild(value.CreateNavigator());
            }
            else if (value != null)
            {
                node.ReplaceSelf(value.CreateNavigator());
            }
            else
            {
                node.DeleteSelf();
            }
        }
예제 #9
0
        private void WriteSimple(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
        {
            var node = result.GetNavigator(true);

            if (result.Type.IsEnum || result.Type == typeof(Guid))
            {
                node.SetTypedValue(value.ToString());
            }
            else
            {
                try
                {
                    node.SetTypedValue(value);
                }
                catch (InvalidCastException)
                {
                    DefaultXmlSerializer.Instance.WriteObject(result, node, value);
                }
            }
        }
예제 #10
0
        private object ReadArray(XPathResult result, IDictionaryAdapter dictionaryAdapter)
        {
            if (result.Type == typeof(byte[]))
            {
                XPathNavigator node;
                if (result.GetNavigator(false, true, out node) && node != null)
                {
                    return(Convert.FromBase64String(node.InnerXml));
                }
                return(null);
            }
            var itemType  = result.Type.GetElementType();
            var itemNodes = result.GetNodes(itemType, type => dictionaryAdapter.GetXmlMeta(type));

            if (itemNodes == null)
            {
                return(null);
            }
            var items = itemNodes.Select(node => ReadProperty(node, false, dictionaryAdapter)).ToArray();
            var array = Array.CreateInstance(itemType, items.Length);

            items.CopyTo(array, 0);
            return(array);
        }