Writing XmlSchema to a MemoryStream fails when starting with Nunit

I am trying to convert an XmlSchema object to a string.
I create a simple XmlSchema, compile it, and then convert it as follows:

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 working as a console application, everything works fine, but when I start from Nunit, I get an exception in "xmlSchema.Write (memStream)"; line.

Exception: Error creating XML document.

Internal exception

: Common Language Runtime detects an invalid program.

+4
source share
1 answer

Your problem probably won't be fixed, but you might want to wrap your threads around you.

 // 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; 

In this way, threads are removed properly. Perhaps this is what NUnit is complaining about.

+2
source

All Articles