Stream was not readable error

I am getting the message "Stream was not readable" on the statement:

using (StreamReader sr = new StreamReader(ms))

I have tried the tips posted here without success. Thanks for the help.

This is my code:

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Conflict));
//Serialize Conflicts array to memorystream as XML
using (MemoryStream ms = new MemoryStream())
{
    using (StreamWriter sw = new StreamWriter(ms))
    {
        foreach (Conflict ct in Conflicts)      
            xmlSerializer.Serialize(sw, ct);

        sw.Flush(); //Site tip
        ms.Position = 0;  //Site tip
    }
    //Retrieve memory stream to string
    using (StreamReader sr = new StreamReader(ms))
    {
        string conflictXml = String.Format(CultureInfo.InvariantCulture, "{0}</NewDataSet>",

Answers


When this block of code completes, it will also dispose the attached MemoryStream

using (StreamWriter sw = new StreamWriter(ms))
{
    foreach (Conflict ct in Conflicts)
        xmlSerializer.Serialize(sw, ct);
    sw.Flush(); //Site tip
    ms.Position = 0;  //Site tip
}

Remove the using statement, and dispose the stream manually after you are done with it

StreamWriter sw = new StreamWriter(ms);

foreach (Conflict ct in Conflicts)
    xmlSerializer.Serialize(sw, ct);
sw.Flush(); //Site tip
ms.Position = 0;  //Site tip

// other code that uses MemoryStream here...

sw.Dispose();

Try this instead (assuming Conflicts is of type List<Conflict>):

XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Conflict>));
StringWriter sw = new StringWriter();
xmlSerializer.Serialize(sw, Conflicts);
string conflictXml = sw.GetStringBuilder().ToString();

Need Your Help

Django + MySQL on Mac OS 10.6.2 Snow Leopard

python mysql django osx-snow-leopard

There were some excellent answers to this question already, however, they are now outdated.

Using RabbitMQ is there a way to look at the queue contents without a dequeue operation?

python rabbitmq esb amqp

As a way to learn RabbitMQ and python I'm working on a project that allows me to distribute h264 encodes between a number of computers. The basics are done, I have a daemon that runs on Linux or Mac