public async Task <string> SerializeMarshallAsync <T>(T t)
        {
            // if you can, only create one serializer
            // creating serializers is an expensive
            // operation and can be slow
            global::System.Xml.Serialization.XmlSerializer serializer = null;

            serializer = new global::System.Xml.Serialization.XmlSerializer(typeof(T));

            // we will write the our result to memory
            //await using
            global::System.IO.MemoryStream stream = new global::System.IO.MemoryStream();
            // the string will be utf8 encoded
            //await using
            global::System.IO.StreamWriter writer = new global::System.IO.StreamWriter
                                                    (
                stream,
                global::System.Text.Encoding.UTF8
                                                    );

            // here we go!
            serializer.Serialize(writer, t);

            // flush our write to make sure its all there
            await writer.FlushAsync();

            // reset the stream back to 0
            stream.Position = 0;

            using global::System.IO.StreamReader reader = new global::System.IO.StreamReader(stream);

            string xml = await reader.ReadToEndAsync();

            return(xml);
        }
Exemplo n.º 2
0
        public async Task <string> SerializeMarshallAsync <T>(T t)
        {
            global::System.IO.MemoryStream stream = new global::System.IO.MemoryStream();
            await global::System.Text.Json.JsonSerializer.SerializeAsync(stream, t);

            // reset the stream back to 0
            stream.Position = 0;

            using global::System.IO.StreamReader reader = new global::System.IO.StreamReader(stream);

            // we reread the stream to a string
            string json = await reader.ReadToEndAsync();

            return(json);
        }
        public async Task <string> SerializeMarshallAsync <T>(T t)
        {
            string result = null;

            using (global::System.IO.MemoryStream ms = new global::System.IO.MemoryStream())
            {
                global::System.Runtime.Serialization.DataContractSerializer serializer = null;
                serializer = new global::System.Runtime.Serialization.DataContractSerializer(typeof(T));

                serializer.WriteObject(ms, t);

                ms.Seek(0, global::System.IO.SeekOrigin.Begin);

                using (global::System.IO.StreamReader sr = new global::System.IO.StreamReader(ms))
                {
                    result = await sr.ReadToEndAsync();
                }
            }

            return(result);
        }