尝试将 xsl 文件和 xml 文件转换为 html,然后将其显示在 WebBrowser 对象中

发布于 2024-11-01 07:39:21 字数 2403 浏览 0 评论 0原文

因此,我尝试转换使用 xsl 文件的 xml 文件,然后将这两个文件转换为 html,我可以将其绑定到 WebBrowser 对象。这是我到目前为止不起作用的:

        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:如果我点击“否”来调试错误,页面将按我的意愿显示。

So I am trying to convert a xml file that uses an xsl file then convert both of those to html which I can bind to a WebBrowser object. Here is what I have so far that isnt working:

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

    }

This code is in a function that returns htmlToPost and the returning data is bound to the WebBrowser like this:

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

I know that XslTransform has been deprecated but all the examples online use it so thats why I am using it.

The error i get is this :

A Runtime Error has occured. Do you wish to debug?

Line: 177
Error: Expected ')'

It happens when this code tries to execute:

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

Thanks in advance for any help you can give me.

EDIT #1: If i hit no to debugging the errors the page shows as i would like.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是男神闪亮亮 2024-11-08 07:39:21

我在我的项目中执行此操作

创建一个临时文件:

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 传递给 web 浏览器控件。

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

I do this in my project

Make a temp file:

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

Save the content:

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

Then pass a MemoryStream to the webbrowser control.

webBrowser1.DocumentStream = new MemoryStream(File.ReadAllBytes(ReportTempPath));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文