public static void SerializeObject( this IXmlSerializerInternal serializer, Stream stream, object instance, Encoding encoding, Formatting formatting, ISerializeOptions options) { options = options.WithNewSerializationState(); StreamWriter streamWriter = null; try { streamWriter = new StreamWriter(stream, encoding ?? Encoding.UTF8); var xmlWriter = new XSerializerXmlTextWriter(streamWriter, options) { Formatting = formatting }; serializer.SerializeObject(xmlWriter, instance, options); xmlWriter.Flush(); } finally { if (streamWriter != null) { streamWriter.Flush(); } } }
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(); } }
public static void SerializeObject( this IXmlSerializerInternal serializer, TextWriter writer, object instance, Formatting formatting, ISerializeOptions options) { options = options.WithNewSerializationState(); var xmlWriter = new XSerializerXmlTextWriter(writer, options) { Formatting = formatting }; serializer.SerializeObject(xmlWriter, instance, options); xmlWriter.Flush(); }
public static string SerializeObject( this IXmlSerializerInternal serializer, object instance, Encoding encoding, Formatting formatting, ISerializeOptions options) { options = options.WithNewSerializationState(); var sb = new StringBuilder(); using (var stringWriter = new StringWriterWithEncoding(sb, encoding ?? Encoding.UTF8)) { using (var xmlWriter = new XSerializerXmlTextWriter(stringWriter, options)) { xmlWriter.Formatting = formatting; serializer.SerializeObject(xmlWriter, instance, options); } } return(sb.ToString()); }
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(); } }