IEnumerable <T> ApplyUnpackRepackXml <T>(IIntermediateDataSerializer formatter, T input)
        {
            XElement intermediate;

            using (var stream = new MemoryStream())
            {
                formatter.Serialize(input, stream, typeof(T));
                stream.Seek(0, SeekOrigin.Begin);
                intermediate = formatter.UnpackXml(stream);
            }

            // Direct
            using (var stream = new MemoryStream())
            {
                formatter.RepackXml(intermediate, stream);
                stream.Seek(0, SeekOrigin.Begin);
                yield return((T)formatter.Deserialize(stream, typeof(T)));
            }

            // ToString/Parse Roundtrip
            {
                var text = intermediate.ToString();
                var xml  = XElement.Parse(text);

                using (var stream = new MemoryStream())
                {
                    formatter.RepackXml(xml, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    yield return((T)formatter.Deserialize(stream, typeof(T)));
                }
            }

            // Save/Load Roundtrip
            {
                string text;
                using (var writer = new StringWriter())
                {
                    intermediate.Save(writer);
                    text = intermediate.ToString();
                }

                XElement xml;
                using (var reader = new StringReader(text))
                {
                    xml = XElement.Load(reader);
                }

                using (var stream = new MemoryStream())
                {
                    formatter.RepackXml(xml, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    yield return((T)formatter.Deserialize(stream, typeof(T)));
                }
            }
        }
Пример #2
0
        public static Result <XElement, Exception> TryUnpackXml(this IIntermediateDataSerializer serializer, Stream source)
        {
            var position = source.Position;

            try
            {
                var result = serializer.UnpackXml(source);
                if (result == null)
                {
                    return(Result <XElement, Exception> .CreateError(new SerializationException("Serializer returned null")));
                }

                return(Result <XElement, Exception> .CreateSuccess(result));
            }
            catch (Exception e)
            {
                return(Result <XElement, Exception> .CreateError(e));
            }
            finally
            {
                source.Position = position;
            }
        }