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)); }
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); }
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; } } } }
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); } } }
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; } }
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()); }
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); }
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(); } }
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); } } }
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); }