I was looking for a way to print my objects in a project and this post should reflect what I've come up with so far. It's without a doubt not the best way to do it but for now it gets the job done.
What I basically do is make an XML file of the objects I am willing to display and then convert them to html. I have created an XSL file, which functions as template, that will generate the html file using the xml data file.
Once the HTML is created we use common Printing provided by our Web browser.
Objects => XML => XML+XSL => HTML => Web Browser Print
You could ask yourself : "why the hell should I go through all this hassle just to make a html file?". Well, I find it very handy to be able to write my objects to xml. It allows you to easily share, sync and validate data with other applications. So once you got your xml exports available on all your objects you can easily convert them to html and use that as your printing environment.
This example only covers the basic idea and should be polished once you descide to use this inside a real/big project. Even the concept of writing your data to an xml file before you generate your html is questionable.
Anyway, I am pretty sure there are many things you could change or even improve to get a simular result.
Following example is made in C# using a console application and reflects the basic principle that is used to accomplish the html result.
This example only covers the basic idea and should be polished once you descide to use this inside a real/big project. Even the concept of writing your data to an xml file before you generate your html is questionable.
Anyway, I am pretty sure there are many things you could change or even improve to get a simular result.
Following example is made in C# using a console application and reflects the basic principle that is used to accomplish the html result.
Throughout the example I used an Employee class that will represent the objects we are about to export to an HTML file.
1. Create a c# console application
2. Inside the console application create an Employee class
class Employee
{
int id;
string firstName;
string lastName;
int salary;
public Employee(int id, string firstname, string lastname, int salary)
{
this.id = id;
this.firstName = firstname;
this.lastName = lastname;
this.salary = salary;
}
public int Id { get { return id; } }
public string FirstName { get { return firstName; } }
public string LastName { get { return lastName; } }
public int Salary { get { return salary; } }
}
3. The main method of our console application holds the rest of our code used within C#.
static void Main(string[] args)
{
//The Array that will hold our Employee objects
Employee[] employees = new Employee[2];
//Adding 2 objects to our previously created Array
employees[0] = new Employee(1, "Robby", "Cattoor", 200);
employees[1] = new Employee(2, "Bjorn", "Cattoor", 500);
//We use the using statement to make sure the object is correctly closed and disposed after use.
using(XmlWriter writer = XmlWriter.Create(@"d:\employees.xml"))
{
//Start the xml document
writer.WriteStartDocument();
//Start an element that will hold all employees
writer.WriteStartElement("Employees");
foreach(Employee employee in employees)
{
//Start an employee element
writer.WriteStartElement("Employee");
writer.WriteElementString("Id",employee.Id.ToString());
writer.WriteElementString("Firstname",
employee.FirstName.ToString());
writer.WriteElementString("Lastname",
employee.LastName.ToString());
writer.WriteElementString("Salary",employee.Salary.ToString());
//Close the employee element
writer.WriteEndElement();
}
//Close the employees element
writer.WriteEndElement();
//Close the document
writer.WriteEndDocument();
}
//Declare and create a new XSLT processor object that implements the XSLT version 1.0 recommendation
XslCompiledTransform transform = new XslCompiledTransform();
//Load the XSL we created
transform.Load(@"d:\employees.xsl");
//Call the transform method to transform you xml to an xml output
transform.Transform(@"d:\employees.xml", @"d:\result.html");
}
Note : Adjust the location of your xml, xsl and html accordingly
4. Leaves us with the definition of our XSL file that will function as our style sheet and template.
<?xml version="1.0" encoding="utf-8"?>
<!--Stylesheet version & namespace reference-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--Template definition-->
<xsl:template match="/">
<!--Html opening tag-->
<html>
<!--Body opening tag-->
<body>
<!--Html title-->
<H2>Testing XSLT on Employees</H2>
<!--Table definition-->
<table border ="1">
<!--Table first row containing 4 headers-->
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Salary</th>
</tr>
<!--Foreach loop selecting all employees-->
<xsl:for-each select="Employees/Employee">
<!--Each employee will result in a row filling each cell in each column containing our Employee data-->
<tr>
<td><xsl:value-of select="Id"/></td>
<td><xsl:value-of select="Firstname"/></td>
<td><xsl:value-of select="Lastname"/></td>
<td><xsl:value-of select="Salary"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Running the program will result in a xml-file and a html file at the location specified inside your static Main() method. You could open the html file inside your default browser after creation or use a .Net Web Browser control to display your result. Printing can now be done using the built-in web browser printing capabilities. Further styling can be done inside the XSL-file inline or using CSS.
Result Code :