【问题标题】:Trying to convert an xsl file and an xml file to html and then display that in a WebBrowser object尝试将 xsl 文件和 xml 文件转换为 html,然后将其显示在 WebBrowser 对象中
【发布时间】:2011-04-11 23:10:06
【问题描述】:

所以我试图转换一个使用 xsl 文件的 xml 文件,然后将这两个文件都转换为我可以绑定到 WebBrowser 对象的 html。这是我到目前为止没有工作的内容:

        protected string ConvertXSLAndXMLToHTML(string xmlSource, string xslSource)
        {

        string resultDoc = Application.StartupPath + @"\result.html";
        string htmlToPost;


        try
        {
            XPathDocument myXPathDoc = new XPathDocument(xmlSource);
            XslTransform myXslTrans = new XslTransform();

            //load the Xsl 
            myXslTrans.Load(xslSource);

            //create the output stream
            XmlTextWriter myWriter = new XmlTextWriter(resultDoc, null);

            //do the actual transform of Xml
            myXslTrans.Transform(myXPathDoc, null, myWriter);

            myWriter.Close();

            StreamReader stream = new StreamReader(resultDoc);
            htmlToPost = stream.ReadToEnd();
            stream.Close();

            File.Delete(resultDoc);

            return (htmlToPost);


         }

         catch (FileNotFoundException fileEx)
         {
            MessageBox.Show("File Not Found: " + fileEx.FileName, "File Not Found Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return null;
        }


        catch (Exception ex)
        {
            MessageBox.Show("General Exception: " + ex.Message, "Exception Thrown" , MessageBoxButtons.OK, MessageBoxIcon.Error);
            return null;
        }

    }

这段代码在一个返回 htmlToPost 的函数中,返回的数据像这样绑定到 WebBrowser:

            // webReport is the WebBrowser object
            // htmlString is the html passed to the function
            // that will bind the html text to the WebBrowser object

            webReport.Navigate("about:blank");
            IHTMLDocument2 test = (IHTMLDocument2)webReport.Document.DomDocument;
            test.write(htmlString);
            webReport.Document.Write(string.Empty);
            webReport.DocumentText = htmlString;

我知道 XslTransform 已被弃用,但所有在线示例都使用它,所以这就是我使用它的原因。

我得到的错误是这样的:

发生了运行时错误。您要调试吗?

行:177 错误:应为 ')'

当这段代码尝试执行时会发生这种情况:

        IHTMLDocument2 test = (IHTMLDocument2)webReport.Document.DomDocument;
        test.write(htmlString);  //this is the actual line that causes the error and it traces into assembly code.

提前感谢您能给我的任何帮助。

编辑#1:如果我拒绝调试页面显示的错误,我想。

【问题讨论】:

  • 可能是 XML 或 XSL 本身的有效性问题
  • 如果我拒绝错误,内容显示正确。有没有办法抑制错误?

标签: c# .net html browser xslt


【解决方案1】:

我在我的项目中这样做

制作一个临时文件:

string ReportTempPath = Path.Combine(Path.GetTempPath(), "pubreport" + Guid.NewGuid().ToString() + ".html");

保存内容:

var root =
                new XElement(ns + "html",
                    new XElement(ns + "head",
                        new XElement(ns + "title", "Publisher Report"),

// ...

var docType = new XDocumentType("html",
              "-//W3C//DTD XHTML 1.0 Strict//EN",
              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null);

    var doc =
        new XDocument(
            new XDeclaration("1.0", "utf-8", "no"),
            docType,
            root
        );

    doc.Save(path);

然后将 MemoryStream 传递给 webbrowser 控件。

webBrowser1.DocumentStream = new MemoryStream(File.ReadAllBytes(ReportTempPath));

【讨论】:

  • 我的实现有效,我正在寻找要解决的错误类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 2014-09-23
相关资源
最近更新 更多