Exemplo n.º 1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="producer">A delegate that should populate first parameter with new portion of data. And return false if there is no data left</param>
 /// <param name="desiredBufferedLength">Desired size of internal buffer. May have major impact on overall performance</param>
 public ProducerConsumerStream(Func <TextWriter, bool> producer, int desiredBufferedLength)
 {
     _bufferList            = new LinkedList <byte[]>();
     _producer              = producer;
     _desiredBufferedLength = desiredBufferedLength;
     _writer = new CustomTextWriter(_bufferList, _desiredBufferedLength);
 }
Exemplo n.º 2
0
        /// <summary>
        /// 对象序列化
        /// </summary>
        /// <typeparam name="T">需要序列化的对象类型</typeparam>
        /// <param name="obj">需要序列化的对象</param>
        /// <param name="omitXmlDeclaration">是否忽略文件头顶部的申明</param>
        /// <returns>Xml字符串</returns>
        public static string SerializeXml <T>(this T obj, bool omitXmlDeclaration = false)
        {
            var textWriter        = new CustomTextWriter(GlobalSerializationOptions.DefaultEncoding);
            var xmlWriterSettings = new XmlWriterSettings
            {
                Encoding           = GlobalSerializationOptions.DefaultEncoding,
                OmitXmlDeclaration = omitXmlDeclaration,
                ConformanceLevel   = ConformanceLevel.Auto,
                Indent             = GlobalSerializationOptions.XmlIndentFormat
            };

            using (var xw = XmlWriter.Create(textWriter, xmlWriterSettings))
            {
                var xs = new XmlSerializer(obj.GetType());
                xs.Serialize(xw, obj);
            }

            return(textWriter.ToString());
        }