/// <summary>
/// Writes the XML elements to the xml writer.
/// </summary>
/// <param name="writer">The writer.</param>
/// <param name="type">The type.</param>
private void WriteXmlElements(XmlWriter writer, Type type)
{
IEnumerable <KeyValuePair <string, object> > propertiesAsElements;
lock (_propertyValuesLock)
{
propertiesAsElements = (from propertyValue in _propertyBag.GetAllProperties()
where PropertyDataManager.IsPropertyNameMappedToXmlElement(type, propertyValue.Key)
select propertyValue);
}
foreach (var propertyAsElement in propertiesAsElements)
{
var propertyInfo = GetPropertyInfo(propertyAsElement.Key);
if (propertyInfo == null)
{
continue;
}
if (propertyAsElement.Value == null)
{
continue;
}
string serializationPropertyName = propertyAsElement.Key;
serializationPropertyName = PropertyDataManager.MapPropertyNameToXmlElementName(type, serializationPropertyName);
var propertyType = propertyInfo.PropertyType;
var propertyTypeToSerialize = propertyAsElement.Value.GetType();
var serializer = SerializationHelper.GetDataContractSerializer(GetType(), propertyTypeToSerialize, serializationPropertyName, propertyAsElement.Value);
if (propertyType != propertyTypeToSerialize)
{
Log.Debug("Property type for property '{0}' is '{1}' but registered as '{2}', adding type info for deserialization",
propertyInfo.Name, propertyTypeToSerialize.FullName, propertyType.FullName);
serializer.WriteStartObject(writer, propertyAsElement.Value);
writer.WriteAttributeString("ctl", "type", null, propertyTypeToSerialize.FullName);
serializer.WriteObjectContent(writer, propertyAsElement.Value);
serializer.WriteEndObject(writer);
}
else
{
serializer.WriteObject(writer, propertyAsElement.Value);
}
}
}