private void SerializeExpandoObject(XSerializerXmlTextWriter writer, IDictionary <string, object> expando, ISerializeOptions options) { if (expando == null && !options.ShouldEmitNil) { return; } writer.WriteStartDocument(); writer.WriteStartElement(_options.RootElementName); writer.WriteDefaultDocumentNamespaces(); using (writer.WriteDefaultNamespace(_options.DefaultNamespace)) { if (expando == null) { writer.WriteNilAttribute(); writer.WriteEndElement(); return; } var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options); foreach (var property in expando) { if (property.Value == null) { continue; } IXmlSerializerInternal serializer; if (property.Value is ExpandoObject) { serializer = DynamicSerializer.GetSerializer <ExpandoObject>(null, _options.WithRootElementName(property.Key)); } else { serializer = CustomSerializer.GetSerializer(property.Value.GetType(), null, _options.WithRootElementName(property.Key)); } serializer.SerializeObject(writer, property.Value, options); } if (setIsEncryptionEnabledBackToFalse) { writer.IsEncryptionEnabled = false; } writer.WriteEndElement(); } }
public void SerializeObject(XSerializerXmlTextWriter writer, object instance, ISerializeOptions options) { if (instance == null && !options.ShouldEmitNil) { return; } writer.WriteStartDocument(); writer.WriteStartElement(_options.RootElementName); writer.WriteDefaultDocumentNamespaces(); using (writer.WriteDefaultNamespace(_options.DefaultNamespace)) { if (instance == null) { writer.WriteNilAttribute(); writer.WriteEndElement(); return; } var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options); foreach (var item in GetDictionaryEntries(instance)) { writer.WriteStartElement("Item"); if (item.Key != null) { _keySerializer.SerializeObject(writer, item.Key, options); } if (item.Value != null) { _valueSerializer.SerializeObject(writer, item.Value, options); } writer.WriteEndElement(); } if (setIsEncryptionEnabledBackToFalse) { writer.IsEncryptionEnabled = false; } writer.WriteEndElement(); } }
private void WriteElement(XSerializerXmlTextWriter writer, Action <XSerializerXmlTextWriter> writeValueAction, ISerializeOptions options) { writer.WriteStartElement(_elementName); writer.WriteDefaultDocumentNamespaces(); var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options); writeValueAction(writer); if (setIsEncryptionEnabledBackToFalse) { writer.IsEncryptionEnabled = false; } writer.WriteEndElement(); }
public void SerializeObject(XSerializerXmlTextWriter writer, object instance, ISerializeOptions options) { if (instance == null && !options.ShouldEmitNil) { return; } if (_options.RootElementName != null) { writer.WriteStartDocument(); writer.WriteStartElement(_options.RootElementName); writer.WriteDefaultDocumentNamespaces(); writer.WriteDefaultNamespace(_options.DefaultNamespace).Dispose(); } if (instance == null) { writer.WriteNilAttribute(); } else { var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options); foreach (var item in (IEnumerable)instance) { _itemSerializer.SerializeObject(writer, item, options); } if (setIsEncryptionEnabledBackToFalse) { writer.IsEncryptionEnabled = false; } } if (_options.RootElementName != null) { writer.WriteEndElement(); } }