Category / Section
How to convert an XML document to HTML using XSLT and print the document in a console in WinForms HTMLUIControl?
1 min read
Convert an XML to HTML using XSLT
To convert the xml document to htm using XSLT and can be easily printed in a console application. Here is the code to convert the xml document to htm file.
Note:
This code has to be placed in try catch block.
C#
HTMLUIControl htmlu=new HTMLUIControl(); XPathDocument myXPathDocument = new XPathDocument("..//..//Albums.xml"); XslTransform myXslTransform = new XslTransform(); XmlTextWriter writer = new XmlTextWriter("result.htm", null); myXslTransform.Load("..\\..\\HhRunSheet.xslt"); myXslTransform.Transform(myXPathDocument, null, writer); writer.Close(); StreamReader stream = new StreamReader ("result.htm"); Console.Write("**This is result document**\n\n"); Console.Write(stream.ReadToEnd());
VB
Dim myXPathDocument As XPathDocument = New XPathDocument("..//..//RunSheetXml_02_Albums.xml") Dim myXslTransform As XslTransform = New XslTransform() Dim writer As XmlTextWriter = New XmlTextWriter("result.htm", Nothing) myXslTransform.Load("..\..\HhRunSheet.xslt") myXslTransform.Transform(myXPathDocument, Nothing, writer) writer.Close() Dim stream As StreamReader = New StreamReader ("result.htm") Console.Write("**This is result document**" & Constants.vbLf + Constants.vbLf) Console.Write(stream.ReadToEnd())
Here is the code to print the generated htm file using the printer.
C#
htmlu.LoadHTML("result.htm"); InputHTML document = new InputHTML( "result.htm", htmlu.FormatManager ); htmlu.PrepareDocument(document, true); //loading the generated htm file in HTMLUI control. htmlu.LoadHTML("result.htm"); htmlu.AutoScrollMinSize = document.AutoScrollMinSize; HTMLUIPrintDocument pd; pd=new HTMLUIPrintDocument(htmlu.Document); PrintDialog dg = new PrintDialog(); dg.Document = pd; dg.AllowSomePages = true; //To print all the pages in document. dg.PrinterSettings.PrintRange=System.Drawing.Printing.PrintRange.AllPages; pd.Print();
VB
htmlu.LoadHTML("result.htm") Dim document As InputHTML = New InputHTML("result.htm", htmlu.FormatManager) htmlu.PrepareDocument(document, True) htmlu.LoadHTML("result.htm") htmlu.AutoScrollMinSize = document.AutoScrollMinSize Dim pd As HTMLUIPrintDocument pd = New HTMLUIPrintDocument(htmlu.Document) Dim dg As PrintDialog = New PrintDialog() dg.Document = pd dg.AllowSomePages = True dg.PrinterSettings.PrintRange=System.Drawing.Printing.PrintRange.AllPages pd.Print()
Reference link: https://help.syncfusion.com/windowsforms/html-viewer/printing