public static void Serialize <T>(Stream stream, T value, ITextSerializer <T> textSerializer)
 {
     using (var textWriter = StreamWriterHelper.NewLeaveOpen(stream))
     {
         textSerializer.Serialize(textWriter, value);
     }
 }
Ejemplo n.º 2
0
        public static void Serialize <T>(Stream stream, T obj)
        {
            using (var writer = StreamWriterHelper.NewLeaveOpen(stream))
            {
                var serializer = new JsonSerializer();

                serializer.Serialize(writer, obj);
            }
        }
Ejemplo n.º 3
0
        public static void Serialize <T>(Stream stream, T obj)
        {
            using (var writer = StreamWriterHelper.NewLeaveOpen(stream))
            {
                var xmlSerializer = new XmlSerializer(typeof(T));

                xmlSerializer.Serialize(writer, obj);
            }
        }
        private static void EnsureNoBOMProduced()
        {
            var tempFilePath = @"C:\Temp\temp.txt";

            using (var fileStream = FileStreamHelper.NewWrite(tempFilePath))
                using (var writer = StreamWriterHelper.NewLeaveOpen(fileStream))
                {
                    writer.WriteLine("Hello world! (BOM?)");
                }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Writes XML without any namespaces.
        /// </summary>
        public static void SerializeWithoutNamespaces <T>(Stream stream, T obj, XmlWriterSettings xmlWriterSettings)
        {
            // Required to suppress the "xmlns:xsi" and "xmlns:xsd" on the root element.
            var xmlSerializerNamespaces = new XmlSerializerNamespaces();

            xmlSerializerNamespaces.Add(String.Empty, String.Empty);

            using (var writer = StreamWriterHelper.NewLeaveOpen(stream))
                using (var xmlWriter = XmlWriter.Create(writer, xmlWriterSettings))
                    using (var namespaceSuppressingWriter = new NamespaceSupressingXmlWriter(xmlWriter)) // Suppresses all element namespaces.
                    {
                        var xmlSerializer = new XmlSerializer(typeof(T));

                        xmlSerializer.Serialize(namespaceSuppressingWriter, obj, xmlSerializerNamespaces);
                    }
        }
        public void Serialize(Stream stream, T value)
        {
            StreamWriter GetStreamWriter()
            {
                var streamWriter = this.StreamSerializerOptions.Value.AddByteOrderMark
                    ? StreamWriterHelper.NewLeaveOpenAddBOM(stream)
                    : StreamWriterHelper.NewLeaveOpen(stream)
                ;

                return(streamWriter);
            }

            using (var textWriter = GetStreamWriter())
            {
                this.TextSerializer.Serialize(textWriter, value);
            }
        }
        internal void Log(LogLevel logLevel, string categoryName, EventId eventId, string formattedStateAndException)
        {
            lock (this.LockObject)
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var writer = StreamWriterHelper.NewLeaveOpen(memoryStream))
                    {
                        this.PerformLog(writer, logLevel, categoryName, eventId, formattedStateAndException);
                    }

                    using (var fileStream = new FileStream(this.LogFilePath, FileMode.OpenOrCreate))
                    {
                        fileStream.Seek(0, SeekOrigin.End);

                        memoryStream.Seek(0, SeekOrigin.Begin);
                        memoryStream.CopyTo(fileStream);
                    }
                }
            }
        }