private object deserializeXmlElement(Type memberType, XmlElement xmlElement) { if (memberType == typeof(string)) { return(xmlElement.InnerText); } if ((xmlElement.InnerText == string.Empty) && PRIMITIVE_TYPES_NULL_IS_0.Contains(memberType)) { return(0); } if (memberType.IsArray) { List <XmlElement> childElements = new List <XmlElement>(); foreach (XmlNode childNode in xmlElement.ChildNodes) { if (childNode.NodeType == XmlNodeType.Element) { childElements.Add((XmlElement)childNode); } } return(deserializeArray(memberType, childElements)); } if (memberType.IsEnum) { return(Enum.Parse(memberType, xmlElement.InnerText)); } if (Type.GetTypeCode(memberType) == TypeCode.Object) { IValueXmlSerializer serializer = GetSerializerForType(memberType); if (serializer == null) { return(xmlElement.InnerText); } return(serializer.DeserializeItem(xmlElement.OfType <XmlElement>().FirstOrDefault())); } return(Convert.ChangeType(xmlElement.InnerText, memberType)); }
private object deserializeXmlElement(MemberInfo memberInfo, Type memberType, XmlElement xmlElement, object parentItem, int arrayDimension = 0) { if (memberType == typeof(string)) { return(xmlElement.InnerText); } if ((xmlElement.InnerText == string.Empty) && PRIMITIVE_TYPES_NULL_IS_0.Contains(memberType)) { return(0); } bool isCollection = memberType.GetInterfaces().Any(interfaceType => interfaceType.IsGenericType && (interfaceType.GetGenericTypeDefinition() == typeof(ICollection <>))); if (memberType.IsArray || isCollection) { List <XmlElement> childElements = new List <XmlElement>(); foreach (XmlNode childNode in xmlElement.ChildNodes) { if (childNode.NodeType == XmlNodeType.Element) { childElements.Add((XmlElement)childNode); } } return(deserializeArray(memberInfo, memberType, childElements, parentItem, arrayDimension)); } if (memberType.IsEnum) { return(Enum.Parse(memberType, xmlElement.InnerText)); } Type nullableUnderlyingType = Nullable.GetUnderlyingType(memberType); if (nullableUnderlyingType?.IsEnum == true) { if (xmlElement.InnerText == string.Empty) { return(null); } return(Enum.Parse(nullableUnderlyingType, xmlElement.InnerText)); } Type deserializeAsType = memberType; if (Type.GetTypeCode(memberType) == TypeCode.Object) { PersistSubclassAttribute persistSubclassAttribute = memberInfo.GetCustomAttributes <PersistSubclassAttribute>().FirstOrDefault(); if (persistSubclassAttribute != null) // should check if given type is subclass of member type { deserializeAsType = persistSubclassAttribute.SubclassType; } PolymorphFieldAttribute polymorphFieldAttribute = memberInfo.GetCustomAttributes <PolymorphFieldAttribute>().FirstOrDefault(); Dictionary <Type, string> typeStringDictionary = null; if (polymorphFieldAttribute != null) { string itemTypeString = xmlElement.GetAttribute(polymorphFieldAttribute.TypeAttributeName); MethodInfo typeStringDictionaryGetterMethodInfo = parentItem.GetType().GetMethod(polymorphFieldAttribute.TypeStringDictionaryGetterName, memberLookupBindingFlags); typeStringDictionary = typeStringDictionaryGetterMethodInfo.Invoke(parentItem, new object[] { }) as Dictionary <Type, string>; KeyValuePair <Type, string> foundTypeData = typeStringDictionary.FirstOrDefault(kvp => (kvp.Value == itemTypeString)); if (foundTypeData.Key != null) { deserializeAsType = foundTypeData.Key; } } IValueXmlSerializer serializer = GetSerializerForType(deserializeAsType); if (serializer == null) { return(xmlElement.InnerText); } PersistAsAttribute persistData = getPersistDataForMember(memberInfo, null, arrayDimension); XmlElement itemToDeserialize = xmlElement; if (persistData.TagName != null) { itemToDeserialize = itemToDeserialize.OfType <XmlElement>().FirstOrDefault(); } return(serializer.DeserializeItem(itemToDeserialize, parentItem)); } return(Convert.ChangeType(xmlElement.InnerText, deserializeAsType)); }