public SetDeserializationBaseObject ( object obj ) : void | ||
obj | object | The object used as the base object in the next stage of deserialization. |
return | void |
/// <summary> /// Retreives the value of the element from the specified XML element or attribute. /// </summary> /// <param name="o">The object to store the retrieved value at.</param> /// <param name="member">The member of the specified object whose value we intent to retreive.</param> /// <param name="elemValue">The value of the element stored as string.</param> /// <param name="xelemValue">The XML element value to be retreived. If the value to be retreived /// has been stored in an XML attribute, this reference is <c>null</c>.</param> private void RetreiveElementValue(object o, MemberWrapper member, string elemValue, XElement xelemValue) { Type memberType = member.MemberType; // try to retrieve the real-type if specified if (xelemValue != null) { XAttribute realTypeAttribute = xelemValue.Attribute_NamespaceSafe(m_yaxLibNamespaceUri + m_trueTypeAttrName); if (realTypeAttribute != null) { Type realType = ReflectionUtils.GetTypeByName(realTypeAttribute.Value); if (realType != null) { memberType = realType; } } } if (xelemValue != null && XMLUtils.IsElementCompletelyEmpty(xelemValue) && !ReflectionUtils.IsBasicType(memberType) && !member.IsTreatedAsCollection && !member.IsTreatedAsDictionary && !AtLeastOneOfMembersExists(xelemValue, memberType)) { try { member.SetValue(o, member.DefaultValue); } catch { OnExceptionOccurred(new YAXDefaultValueCannotBeAssigned(member.Alias.LocalName, member.DefaultValue), member.TreatErrorsAs); } } else if (memberType == typeof(string)) { if (String.IsNullOrEmpty(elemValue) && xelemValue != null) { if (xelemValue.IsEmpty) elemValue = null; else elemValue = String.Empty; } try { member.SetValue(o, elemValue); } catch { this.OnExceptionOccurred(new YAXPropertyCannotBeAssignedTo(member.Alias.LocalName), this.m_defaultExceptionType); } } else if (ReflectionUtils.IsBasicType(memberType)) { object convertedObj; if (ReflectionUtils.IsNullable(memberType) && String.IsNullOrEmpty(elemValue)) { convertedObj = member.DefaultValue; } else { convertedObj = ReflectionUtils.ConvertBasicType(elemValue, memberType); } try { try { member.SetValue(o, convertedObj); } catch { this.OnExceptionOccurred(new YAXPropertyCannotBeAssignedTo(member.Alias.LocalName), m_defaultExceptionType); } } catch (Exception ex) { if (ex is YAXException) { throw; } this.OnExceptionOccurred(new YAXBadlyFormedInput(member.Alias.LocalName, elemValue), member.TreatErrorsAs); try { member.SetValue(o, member.DefaultValue); } catch { this.OnExceptionOccurred(new YAXDefaultValueCannotBeAssigned(member.Alias.LocalName, member.DefaultValue), m_defaultExceptionType); } } } else if (member.IsTreatedAsDictionary && member.DictionaryAttributeInstance != null) { DeserializeTaggedDictionaryMember(o, member, xelemValue); } else if (member.IsTreatedAsCollection) { DeserializeCollectionMember(o, member, memberType, elemValue, xelemValue); } else { var ser = new YAXSerializer(memberType, m_exceptionPolicy, m_defaultExceptionType, m_serializationOption); ser.SetNamespaceToOverrideEmptyNamespace( member.Namespace. IfEmptyThen(this.TypeNamespace). IfEmptyThenNone()); ser.IsCraetedToDeserializeANonCollectionMember = !(member.IsTreatedAsDictionary || member.IsTreatedAsCollection); if (m_desObject != null) // i.e. it is in resuming mode { ser.SetDeserializationBaseObject(member.GetValue(o)); } object convertedObj = ser.DeserializeBase(xelemValue); m_parsingErrors.AddRange(ser.ParsingErrors); try { member.SetValue(o, convertedObj); } catch { this.OnExceptionOccurred(new YAXPropertyCannotBeAssignedTo(member.Alias.LocalName), this.m_defaultExceptionType); } } }
public void MoreComplexBookTwoResumedDeserializationTest() { string result = @"<MoreComplexBook2 Author_s_Name=""Tom Archer""> <Title>Inside C#</Title> <PublishYear>2002</PublishYear> <Price>30.5</Price> </MoreComplexBook2>"; MoreComplexBook2 book = new MoreComplexBook2(); book.Author = new Author() { Name = null, Age = 40 }; string initialToString = book.ToString(); YAXSerializer serializer = new YAXSerializer(typeof(MoreComplexBook2), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); serializer.SetDeserializationBaseObject(book); MoreComplexBook2 bookResult = (MoreComplexBook2)serializer.Deserialize(result); Assert.AreNotEqual(bookResult.ToString(), initialToString); }