views:

210

answers:

1

Hi,

I'm trying to convert an XmlSchema object to string. I'm building a simple XmlSchema, compiling it, and then converting it as folows:

public string ConvertXmlSchemaToString(XmlSchema xmlSchema)
{
        String SchemaAsString = String.Empty;
        // compile the schema
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.Add(xmlSchema);
        schemaSet.ValidationEventHandler += new ValidationEventHandler(schemaSet_ValidationEventHandler);
        schemaSet.Compile();

        // allocate memory for string output
        MemoryStream MemStream = new MemoryStream(1024);
        xmlSchema.Write(MemStream);
        MemStream.Seek(0, SeekOrigin.Begin);
        StreamReader reader = new StreamReader(MemStream);
        SchemaAsString = reader.ReadToEnd();
        return SchemaAsString;
}

While running as a console app, everything works fine, but when running from Nunit I get an exception in the "xmlSchema.Write(MemStream);" line.

exception is : There was an error generating the XML document.

inner exception is : Common Language Runtime detected an invalid program.

Thanks,

Yossin

A: 

Probably won't fix your problem, but you might want to wrap usings around your streams like so.

// allocate memory for string output
using (MemoryStream MemStream = new MemoryStream(1024))
{
    xmlSchema.Write(MemStream);
    MemStream.Seek(0, SeekOrigin.Begin);
    using (StreamReader reader = new StreamReader(MemStream))
    {
        SchemaAsString = reader.ReadToEnd();
    }
}
return SchemaAsString;

That way the streams are disposed of properly. That might be what NUnit is complaining about.

Cameron MacFarland