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" ?>
<Employees>
  <Employee>
    <Id>1</Id>
    <Name>John</Name>
    <Position>Team Leader</Position>
    <Experience>6</Experience>
  </Employee>
  <Employee>
    <Id>2</Id>
    <Name>Frank</Name>
    <Position>Analyst Programmer</Position>
    <Experience>3</Experience>
  </Employee>
  <Employee>
    <Id>3</Id>
    <Name>Michael</Name>
    <Position>Senior Analyst Programmer</Position>
    <Experience>5</Experience>
  </Employee>
  <Employee>
    <Id>4</Id>
    <Name>Thomas</Name>
    <Position>Senior Analyst Programmer</Position>
    <Experience>4</Experience>
  </Employee>
  <Employee>
    <Id>5</Id>
    <Name>Alan</Name>
    <Position>Trainee Analyst Programmer</Position>
    <Experience>0.5</Experience>
  </Employee>
</Employees>

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();
    dsEmployees.ReadXml(@"DATA\Employees.xml");
 
    EmployeesReport report = new EmployeesReport();
 
    report.SetDataSource(dsEmployees);
    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....

http://www.c-sharpcorner.com/uploadfile/nadeemab/xsd-xml-based-crystal-report-data-source/

http://www.mindstick.com/Articles/3f505f6a-52cb-4837-88b4-fa95f2e00c18/?Crystal%20Report%20with%20XML%20Data%20Source