Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Nunit运行时,将XmlSchema写入MemoryStream失败并出现异常_C#_Nunit_Xsd - Fatal编程技术网

C# 从Nunit运行时,将XmlSchema写入MemoryStream失败并出现异常

C# 从Nunit运行时,将XmlSchema写入MemoryStream失败并出现异常,c#,nunit,xsd,C#,Nunit,Xsd,我正在尝试将XmlSchema对象转换为字符串。 我正在构建一个简单的XmlSchema,对其进行编译,然后按如下方式进行转换: public string ConvertXmlSchemaToString(XmlSchema xmlSchema) { String schemaAsString = String.Empty; // compile the schema XmlSchemaSet schemaSet = new XmlSchemaS

我正在尝试将XmlSchema对象转换为字符串。
我正在构建一个简单的XmlSchema,对其进行编译,然后按如下方式进行转换:

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;
}
作为控制台应用程序运行时,一切正常,但从Nunit运行时,“xmlSchema.Write(memStream);”行中出现异常

例外情况是:生成XML文档时出错


内部异常是:公共语言运行库检测到一个无效程序。

可能无法解决您的问题,但您可能希望像这样将using包装到流中

// 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;
这样可以正确地处理这些流。这可能就是努尼特所抱怨的