In this step-by-step tutorial, you will learn how to create reports from an XML document using the ReportViewer control and Visual Studio 2005.
I have an XML file Data.xml, which looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<Employee Name="Mahesh Chand" Age="30" Phone="6101233333" />
<Employee Name="Rose Garner" Age="56" Phone="2133428778" />
<Employee Name="Amger Jap" Age="22" Phone="9092349800" />
<Employee Name="Mike Gold" Age="35" Phone="9908088823" />
<Employee Name="Renee Flower" Age="19" Phone="4848901003" />
Step 1. Generate and Add the Schema File
My first goal is to generate a schema file, which will represent the data. I take help of the DataSet class and its method WriteXmlSchema. The code listed in Listing 1 reads the Data.xml file and generates a schema file called Data.xsd. This file is created in the Debug folder of your application.
DataSet ds = new DataSet();
Let's add Data.xsd file to your project. Right click on the project in Solution Explorer, select Add >> Existing Item and browse for Data.xsd file and add it to the project.
Now let's Rebuild the project.
Step 2. Create the Report
Now we will add a new report file to the project. Right click on the project in Solution Explorer and select Add >> New Item and select Report from the Items list. It will add Report1.rdlc file to the project.
Once the report is added, our next step is to add a data source. First double click on the Form and select Data Menu item from the Main Menu. Click on Data Menu item and select Add New Data Source item.
It will launch Data Source Configuration Wizard. Select Object from the list and click the Next button on the Wizard. See Figure 1.
On next dialog, you should see all namespaces and classes in your project. Expand your namespace and you will see class Company. See Figure 2.
Select Company class and click the Next button. On next dialog, you will see a confirmation message. Select Finish there and get out of the wizard.
Now double click on the Report1.rdlc file and you should see Figure 3 in your Data Sources window.
Now let's create and format the report.
Drag a Table from the Toolbox and drag Name, Age, and Phone columns from the Data Sources to the report's middle row. As you can see from Figure 4, the name of the column is automatically added to the header (first) row of the report.
Step 3. Create a Report Viewer and Bind the Report
Now open the Form1 again and drag a ReportViewer control from the Toolbox to the Form. Click on the smart tag and select Report1.rdlc from the Choose Report drop down list. See Figure 5.
By doing so, you will see an EmployeeBindingSource control is added at the bottom of the Form. See Figure 6. The BindingSource control provides connection between the data and the ReportViewer control.
Step 4. Fill the Data
Now write the code listed in Listing 2 on the Form's load event handler. This code creates a DataSet, loads the data from Data.xml file and sets EmployeeBindingSource.DataSource as DataSet. The last line is added by you by the designer.
DataSet ds = new DataSet();
EmployeeBindingSource.DataSource = ds;
Step 5. Build and Run
That's all. Build and run the application. The output should look like Figure 7.
In this tutorial, I discussed how we can generate reports from XML documents using Visual Studio 2005 Report Viewer control.
Here is my free book on Report Viewer.