【发布时间】: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 本身的有效性问题
-
如果我拒绝错误,内容显示正确。有没有办法抑制错误?