簡體   English   中英

從Nunit運行時將XmlSchema寫入MemoryStream失敗並出現異常

[英]Writing an XmlSchema to a MemoryStream fails with an exception while running from Nunit

我正在嘗試將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文檔時出錯。

內部異常是:公共語言運行庫檢測到無效程序。

可能無法解決您的問題,但是您可能希望像這樣在流周圍包裝用法。

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

這樣就可以正確處理流。 這可能是NUnit抱怨的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM