コード例 #1
0
        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();
                }
            }
        }
コード例 #2
0
        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();
            }
        }
コード例 #3
0
        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();
        }
コード例 #4
0
        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());
        }
コード例 #5
0
        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();
            }
        }