Saturday, October 25, 2008

Load data from XML file into Crystal Reports

In this post, I will demonstrate how we can display data found in an XML file in a Crystal Report in C# using .NET Framework. It is easier to understand the whole thing when there is no need to write code for database (such as Oracle, Access or SQL) access or adapter or connection objects.

Pre-requisite: Crystal Reports installed on the machine, along with Visual Studio 2005.

1. The first thing is to create a new Windows Application (here I've named it XmlToCrystalReports) in VS2005.

2. I will add an Employees.xml file to the project, under a DATA folder. The xml file will contain data about employees.

Right click on the Windows Application in the Solution Explorer. Select Add -> Add New Item. Choose XML File in the Add New Item form, name it Employees.xml and select Add..

3. Populate the xml file with test data.
<?xml version="1.0" encoding="utf-8" ?>
    <Position>Team Leader</Position>
    <Position>Analyst Programmer</Position>
    <Position>Senior Analyst Programmer</Position>
    <Position>Senior Analyst Programmer</Position>
    <Position>Trainee Analyst Programmer</Position>

4. Next, I will add a Crystal Report to the project.

Right click on the Windows Application in the Solution Explorer. Select Add - Add New Item.

Choose Crystal Report, name it EmployeesReport.rpt, and select Add.

The Crystal Reports Gallery pops up. I will leave the default options and select OK.

The Standard Report Creation Wizard is shown, along with a list of available data sources. Under Project Data -> .NET Objects, double click on XmlToCrystalReports.MainForm.

The ADO.NET form is shown. Browse and select the Employees.xml file created earlier and choose Finish.

And we are back in the Standard Report Creation Wizard. The Employee table is now shown under the Available Data Sources tree. Select the Employee table, move it to the right under the Selected Tables tree and choose Next.

Now, we will need to select the appropriate fields to be displayed in the crystal report. So, move all the fields under the Fields to Display tree on the right and select Finish.

The EmployeesReport.rpt is added to the project and shown in the designer, with a pre-defined layout.

5. Format the crystal report to meet your needs (example, change font size, add title in header)

6. The Solution Explorer will now be as follows:

7. Now to view the report when the application runs, we will need to add a Crystal Report Viewer to the main form. The Crystal Report Viewer component is available in the Toolbox under Crystal Reports.

We can hide the Group Tree found on the left by setting the DisplayGroupTree to false in the Properties Window.

8. The last step is during form load, we will need to (1) load the XML file into a dataset, (2) create an EmployeesReport instance and set the data source to the dataset created, and (3) set the ReportSource of the crystal viewer to the instance of the EmployeesReport.

private void Form1_Load(object sender, EventArgs e)
    DataSet dsEmployees = new DataSet();
    EmployeesReport report = new EmployeesReport();
    crystalReportViewer1.ReportSource = report;

9. Run the application, and (if everything is ok) the Crystal Reports Viewer will display the employee displays found the the xml file.

1 comment:

Anonymous said...

Very nice and informative article that beautifully elaborate the basics of Crystal Report with XML Data Source. Some other good article also explained very well about Crystal report with xml data source. you may check it....