Trying to convert an xsl file and an xml file to html and then display that in a WebBrowser object
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.
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));
精彩评论