Object ToXml method

166 views
Skip to first unread message

Heriyanto Binduni

unread,
Dec 12, 2011, 9:58:59 AM12/12/11
to ServiceStack .NET Open Source REST Web Services Framework
hi, i'm trying to convert my poco object into xml using the code
below:

var orderModel = new OrderModel {
OrderType = "BUY",
Price = 2400,
Lot = 5
};

orderModel.ToJson(); //work fine
orderModel.ToXml(); //error: Cannot access a closed Stream.

but i get an exception message:
Cannot access a closed Stream.

can you tell me what's wrong with my code? fyi, ToJson working fine.
so i suppose to use ToXml method just like ToJson but it seem i'm
wrong.

anyway, tq for your great work.

greeting from indonesia.

heri

Demis Bellot

unread,
Dec 12, 2011, 12:32:11 PM12/12/11
to servic...@googlegroups.com
Hi,

I can't replicate this issue, I've added a test case for it here:

Are you using the latest version? otherwise can you send me a complete failing unit test that fails. Also what is your platform? i.e. Windows or Unix?

Thanks,

Heriyanto Binduni

unread,
Dec 13, 2011, 2:10:58 AM12/13/11
to ServiceStack .NET Open Source REST Web Services Framework
hi demis,
tq for your fast response. i try your test case and i got it like
this:

public void Can_toJson_than_toXml()
{


var orderModel = new OrderModel
{
OrderType = "BUY",
Price = 2400,
Lot = 5
};

var json = orderModel.ToJson();
var fromJson = json.FromJson<OrderModel>();
Assert.That(fromJson.OrderType,
Is.EqualTo(orderModel.OrderType));

var xml = orderModel.ToXml();
var fromXml = xml.FromXml<OrderModel>();
Assert.That(fromXml.OrderType,
Is.EqualTo(orderModel.OrderType));
}

Testing started:

Searching for tests...
Total tests: 1, filtered: 1

Can_toJson_than_toXml has failed:
System.Runtime.Serialization.SerializationException : Error
serializing object of type TestRedis.Program+OrderModel
----> System.ObjectDisposedException : Cannot access a closed
Stream.
C:\src\ServiceStack.Text\src\ServiceStack.Text\XmlSerializer.cs(99,
0) : ServiceStack.Text.XmlSerializer.SerializeToString[T](T from)
C:\src\ServiceStack.Text\src\ServiceStack.Text
\StringExtensions.cs(397, 0) :
ServiceStack.Text.StringExtensions.ToXml[T](T obj)
D:\projects2\TestRedis\TestRedis\Program.cs(50, 0) :
TestRedis.Program.Can_toJson_than_toXml()
C:\src\ServiceStack.Text\src\ServiceStack.Text\XmlSerializer.cs(92,
0) : ServiceStack.Text.XmlSerializer.SerializeToString[T](T from)

Passed: 0, Failed: 1, Ignored: 0
Duration : 1.53349532919818

i used ServiceStack.Text version 3.09 from Nuget package on Windows 7
64 bit, VS 2010 sp1.

thanks,
heri

On Dec 13, 12:32 am, Demis Bellot <demis.bel...@gmail.com> wrote:
> Hi,
>

> I can't replicate this issue, I've added a test case for it here:https://github.com/ServiceStack/ServiceStack.Text/blob/master/tests/S...


>
> Are you using the latest version? otherwise can you send me a complete
> failing unit test that fails. Also what is your platform? i.e. Windows or
> Unix?
>
> Thanks,
>

Demis Bellot

unread,
Dec 13, 2011, 2:23:31 AM12/13/11
to servic...@googlegroups.com
Hi,

In this case I have no idea what the problem is as the same test works on my home Win7 32bit and works Win7 64bit pcs both running VS.NET 2010 (with and without admin mode).

I'd say something is wrong with your environment and the Exception you see is probably hiding an inner exception that's the real cause.
I recommend you try running the source code yourself so you can debug/inspect the errors in your IDE:
https://github.com/ServiceStack/ServiceStack.Text/blob/master/src/ServiceStack.Text/XmlSerializer.cs#L99

        private static readonly XmlWriterSettings XSettings = new XmlWriterSettings();

        public static string SerializeToString<T>(T from)
        {
            try
            {
                using (var ms = new MemoryStream())
                {
                    using (var xw = XmlWriter.Create(ms, XSettings))
                    {
                        var serializer = new DataContractSerializer(from.GetType());
                        serializer.WriteObject(xw, from);
                        xw.Flush();
                        ms.Seek(0, SeekOrigin.Begin);
                        using (var reader = new StreamReader(ms))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SerializationException(string.Format("Error serializing object of type {0}", from.GetType().FullName), ex);
            }
        }

I suspect that it might be a permissions problem as I believe Microsofts Xml DataContractSerializer generates and stores Reflection.Emit'ed assemblies in a tmp folder which you may possibly not have write permission to for some reason - although this is a complete guess so you'll have to debug the issue on your end.

Cheers,

Heriyanto Binduni

unread,
Dec 13, 2011, 5:46:01 AM12/13/11
to ServiceStack .NET Open Source REST Web Services Framework
tq you demis, anyway, i still got the same error for the code you give
below. and also i found someone already open an issue for this
problem.

https://github.com/ServiceStack/ServiceStack.Text/issues/46

tq

On Dec 13, 2:23 pm, Demis Bellot <demis.bel...@gmail.com> wrote:
> Hi,
>

> In this case I have no idea what the problem is as the same test works on
> my home Win7 32bit and works Win7 64bit pcs both running VS.NET 2010 (with
> and without admin mode).
>
> I'd say something is wrong with your environment and the Exception you see
> is probably hiding an inner exception that's the real cause.
> I recommend you try running the source code yourself so you can

> debug/inspect the errors in your IDE:https://github.com/ServiceStack/ServiceStack.Text/blob/master/src/Ser...


>
>         private static readonly XmlWriterSettings XSettings = new
> XmlWriterSettings();
>
>         public static string SerializeToString<T>(T from)
>         {
>             try
>             {
>                 using (var ms = new MemoryStream())
>                 {
>                     using (var xw = XmlWriter.Create(ms, XSettings))
>                     {
>                         var serializer = new
> DataContractSerializer(from.GetType());
>                         serializer.WriteObject(xw, from);
>                         xw.Flush();
>                         ms.Seek(0, SeekOrigin.Begin);
>                         using (var reader = new StreamReader(ms))
>                         {
>                             return reader.ReadToEnd();
>                         }
>                     }
>                 }
>             }
>             catch (Exception ex)
>             {
>                 throw new SerializationException(string.Format("Error
> serializing object of type {0}", from.GetType().FullName), ex);
>             }
>         }
>
> I suspect that it might be a permissions problem as I believe Microsofts
> Xml DataContractSerializer generates and stores Reflection.Emit'ed
> assemblies in a tmp folder which you may possibly not have write permission
> to for some reason - although this is a complete guess so you'll have to
> debug the issue on your end.
>
> Cheers,
>

Heriyanto Binduni

unread,
Dec 13, 2011, 6:21:11 AM12/13/11
to ServiceStack .NET Open Source REST Web Services Framework
hi, i found the same problem here:

http://stackoverflow.com/questions/5480798/c-sharp-using-xmlwriter-create-cannot-access-a-closed-stream

so changing this:

using (var reader = new StreamReader(ms))
{
return reader.ReadToEnd();
}

to this:

var reader = new StreamReader(ms);
return reader.ReadToEnd();

will solve the problem.

regards,
heri

Demis Bellot

unread,
Dec 13, 2011, 11:07:09 AM12/13/11
to servic...@googlegroups.com
Hi thx for the solution it's now checked in and will be available to download in the next version, in the meantime you can download it from: 

Or alternatively if you wish build it yourself.

Thanks for reporting!

Cheers,
Reply all
Reply to author
Forward
0 new messages