1

需要从XML和相应的XSL生成html 报告,但我必须在服务器目录上使用 memorystream 而不是 IO 文件写入。在大多数情况下,我设法创建了一个 xml

MemoryStream ms = new MemoryStream();
XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.Indent = true;
using(XmlWriter writer = XmlWriter.Create(ms,wSettings))
{
      /**
          creating xml here
      **/
      writer.Flush();
      writer.Close();
}
return ms; // returning the memory stream to another function
           // to create html

// This Function creates 
protected string ConvertToHtml(MemoryStream xmlOutput)
{
        XPathDocument document = new XPathDocument(xmlOutput);
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(xmlOutput);
        StringWriter writer = new StringWriter();
        XslCompiledTransform transform = new XslCompiledTransform();
        transform.Load(reportDir + "MyXslFile.xsl");
        transform.Transform(xDoc, null, writer);
        xmlOutput.Position = 1;
        StreamReader sr = new StreamReader(xmlOutput);
        return sr.RearToEnd();
}

在某个地方,我搞砸了创建HTML 报告,无法弄清楚如何将该文件发送到客户端。我没有太多使用内存流的经验。因此,任何帮助将不胜感激。谢谢你。

4

2 回答 2

0

您在这里完全绕过了您的转换:

// This Function creates 
protected string ConvertToHtml(MemoryStream xmlOutput)
{
        XPathDocument document = new XPathDocument(xmlOutput);
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(xmlOutput);

        StringWriter writer = new StringWriter();
        XslCompiledTransform transform = new XslCompiledTransform();
        transform.Load(reportDir + "MyXslFile.xsl");
        transform.Transform(xDoc, null, writer);

        // These lines are the problem
        //xmlOutput.Position = 1;
        //StreamReader sr = new StreamReader(xmlOutput);
        //return sr.RearToEnd();

        return writer.ToString()
}

此外,在编写器上调用 Close 之前调用 Flush 是多余的,因为 Close 意味着刷新操作。

于 2012-05-18T15:32:57.950 回答
0

我不清楚你想要实现什么,但我认为同时使用 XmlDocument 和 XPathDocument 从同一个内存流加载没有意义。我会在从中加载之前将 MemoryStream 设置为位置 0,因此要么让函数创建并写入内存流,以确保它将位置设置为零,或者在调用 XmlDocument 上的 Load 或创建 XPathDocument 之前执行此操作,取决于您要使用的输入树模型。

于 2012-05-18T15:41:32.590 回答