Click here to Skip to main content
12,299,285 members (59,113 online)
Click here to Skip to main content
Articles » Languages » C# » How To » Downloads

Stats

69.6K views
1.1K downloads
48 bookmarked
Posted

Utilize Internet Explorer to display reports using XML and XSL from a Windows application

, 11 Jan 2005 CPOL
How to create a report from XML, format it using an XSL style sheet and display the report in Internet Explorer, all done without saving anything to file.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Xml;

namespace ListCtrlReport
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class FormMain : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListView listView;
		private System.Windows.Forms.ColumnHeader columnHeaderId;
		private System.Windows.Forms.ColumnHeader columnHeaderLName;
		private System.Windows.Forms.ColumnHeader columnHeaderFName;
		private System.Windows.Forms.ColumnHeader columnHeaderPhone;
		private System.Windows.Forms.Button buttonReport;
		private System.Windows.Forms.Button buttonFormat;
		private FormFormat formFormat = new FormFormat();
		private string xsl;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FormMain()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.listView = new System.Windows.Forms.ListView();
			this.columnHeaderId = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderLName = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderFName = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderPhone = new System.Windows.Forms.ColumnHeader();
			this.buttonReport = new System.Windows.Forms.Button();
			this.buttonFormat = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// listView
			// 
			this.listView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					   this.columnHeaderId,
																					   this.columnHeaderLName,
																					   this.columnHeaderFName,
																					   this.columnHeaderPhone});
			this.listView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.listView.Location = new System.Drawing.Point(16, 16);
			this.listView.Name = "listView";
			this.listView.Size = new System.Drawing.Size(500, 154);
			this.listView.TabIndex = 0;
			this.listView.View = System.Windows.Forms.View.Details;
			// 
			// columnHeaderId
			// 
			this.columnHeaderId.Text = "Author Id";
			this.columnHeaderId.Width = 101;
			// 
			// columnHeaderLName
			// 
			this.columnHeaderLName.Text = "Last Name";
			this.columnHeaderLName.Width = 156;
			// 
			// columnHeaderFName
			// 
			this.columnHeaderFName.Text = "First Name ";
			this.columnHeaderFName.Width = 118;
			// 
			// columnHeaderPhone
			// 
			this.columnHeaderPhone.Text = "Phone ";
			this.columnHeaderPhone.Width = 121;
			// 
			// buttonReport
			// 
			this.buttonReport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.buttonReport.Location = new System.Drawing.Point(303, 186);
			this.buttonReport.Name = "buttonReport";
			this.buttonReport.TabIndex = 1;
			this.buttonReport.Text = "Report";
			this.buttonReport.Click += new System.EventHandler(this.buttonReport_Click);
			// 
			// buttonFormat
			// 
			this.buttonFormat.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.buttonFormat.Location = new System.Drawing.Point(159, 186);
			this.buttonFormat.Name = "buttonFormat";
			this.buttonFormat.TabIndex = 2;
			this.buttonFormat.Text = "Format";
			this.buttonFormat.Click += new System.EventHandler(this.buttonFormat_Click);
			// 
			// FormMain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(532, 223);
			this.Controls.Add(this.buttonFormat);
			this.Controls.Add(this.buttonReport);
			this.Controls.Add(this.listView);
			this.MinimumSize = new System.Drawing.Size(400, 150);
			this.Name = "FormMain";
			this.Text = "Authors";
			this.Load += new System.EventHandler(this.FormMain_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new FormMain());
		}

		private void FormMain_Load(object sender, System.EventArgs e)
		{
			// Fill the list control with some data, this just some data from the pubs database.

			AddListViewItem("213-46-8915", "Green", "Marjorie", "415 986-7020"); 
			AddListViewItem("238-95-7766", "Carson", "Cheryl", "415 548-7723"); 
			AddListViewItem("267-41-2394", "O'Leary", "Michael", "408 286-2428"); 
			AddListViewItem("274-80-9391", "Straight", "Dean", "415 834-2919"); 
			AddListViewItem("341-22-1782", "Smith", "Meander", "913 843-0462"); 

			// Initialize our style sheet member variable.

			xsl = formFormat.StyleSheet;
		}
		
		private void AddListViewItem(params string[] subItems)
		{
			// Add new row to list control with given strings. 
			// See http://spaces.msn.com/members/pjsson/Blog/cns!1p76K4WF1ADMWttSKAc6E-Sg!126.entry

			listView.Items.Add(new ListViewItem(subItems));
		}

		private void buttonReport_Click(object sender, System.EventArgs e)
		{
			// Generate XML from list control.

			string xml = @"<?xml version=""1.0"" ?>" + Environment.NewLine;
			xml = "<AUTHORS>";

			foreach (ListViewItem listViewItem in listView.Items)
			{
				xml += @"  <AUTHOR ID=""" + listViewItem.SubItems[0].Text + @""">" + Environment.NewLine;
				xml += @"    <FNAME>" + listViewItem.SubItems[1].Text + @"</FNAME>" + Environment.NewLine;
				xml += @"    <LNAME>" + listViewItem.SubItems[2].Text + @"</LNAME>" + Environment.NewLine;
				xml += @"    <PHONE>" + listViewItem.SubItems[3].Text + @"</PHONE>" + Environment.NewLine;
				xml += @"  </AUTHOR>";
			}      
      				
			xml += @"</AUTHORS>";
			
			// Make html code from the xml using a xsl style sheet.
			
			string html = MakeHTML(xml, xsl);

			// Open up IE and display the html code.

			DisplayHTML(html);
		}

		private void buttonFormat_Click(object sender, System.EventArgs e)
		{
			// Open modal Format dialog.

			if (formFormat.ShowDialog() == DialogResult.OK)
			{
				// User clicked OK to update the xsl.

				xsl = formFormat.StyleSheet;
			}
		}

		public void DisplayHTML(string html)
		{
			// Create a new instance of Internet Explorer.

			SHDocVw.InternetExplorer internetExplorer = new SHDocVw.InternetExplorerClass();

			SHDocVw.IWebBrowser2 webBrowser = (SHDocVw.IWebBrowser2)internetExplorer;

			// Make the web browser visible.

			webBrowser.Visible = true;

			// Display empty page so we have something to manipulate.

			object noValue = System.Reflection.Missing.Value;

			webBrowser.Navigate("about:blank", ref noValue, ref noValue, ref noValue, ref noValue);

			// Get access to the webbrowser's document.

			mshtml.IHTMLDocument2 htmlDoc = internetExplorer.Document as mshtml.IHTMLDocument2;                  

			// Update the document.

			htmlDoc.writeln(html);
			htmlDoc.close();
		}

		public string MakeHTML(string xml, string xsl)
		{       
			// Load the XML string into an XPathDocument.

			StringReader xmlStringReader = new StringReader(xml);
			XPathDocument xPathDocument = new XPathDocument(xmlStringReader);
			
			// Create a reader to read the XSL.

			StringReader xslStringReader = new StringReader(xsl);
			XmlTextReader xslTextReader = new XmlTextReader(xslStringReader);

			// Load the XSL into an XslTransform.

			XslTransform xslTransform = new XslTransform();
			xslTransform.Load(xslTextReader, null, GetType().Assembly.Evidence);

			// Perform the actual transformation and output an HTML string. 
			
			StringWriter htmlStringWriter = new StringWriter();
			xslTransform.Transform(xPathDocument, null, htmlStringWriter, null) ;
			string html = htmlStringWriter.ToString();
			
			// Close all our readers and writers.

			xmlStringReader.Close();
			xslStringReader.Close();
			xslTextReader.Close();
			htmlStringWriter.Close();

			// Done, return the created HTML code.

			return html;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Patric_J
Web Developer
United States United States
I'm a software developer from Sweden who got tired of snow and cold weather and moved to USA. I choose New York City, so I wouldn't totally miss out on snow and cold weather. I work on Wall Street with financial systems (not much else to do in this neighborhood). I primarily use Visual C++/MFC or C#/.NET as development tool.

The picture is of my wife and me in Cannes, France, drinking the most expensive Coke we ever had.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 11 Jan 2005
Article Copyright 2005 by Patric_J
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid