尝试将 xsl 文件和 xml 文件转换为 html,然后将其显示在 WebBrowser 对象中
因此,我尝试转换使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在我的项目中执行此操作
创建一个临时文件:
保存内容:
// ...
然后将 MemoryStream 传递给 web 浏览器控件。
I do this in my project
Make a temp file:
Save the content:
// ...
Then pass a MemoryStream to the webbrowser control.