ARTICLE

Building Reports from XML Documents using ReportViewer

Posted by Mahesh Chand Articles | Reports in VB.NET May 10, 2007
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.
Download Files:
 
Reader Level:

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.

The Data

I have an XML file Data.xml, which looks like this:  

  <?xml version="1.0" encoding="utf-8" ?>

  <Company>

  <Employees>

  <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" />

  </Employees>

  </Company>

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.

Dim ds As DataSet = New DataSet()

ds.ReadXml("Data.xml")

ds.WriteXmlSchema("Data.xsd")


Listing 1.

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.

 

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.



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.


Figure 3.

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.



Figure 4.

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. 



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.

 

Figure 6.

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.

Dim ds As DataSet = New DataSet()

ds.ReadXml("Data.xml")

EmployeeBindingSource.DataSource = ds

Me.reportViewer1.RefreshReport()

Listing 2.

Step 5. Build and Run

That's all. Build and run the application. The output should look like Figure 7.



Figure 7.

Summary

In this tutorial, I discussed how we can generate reports from XML documents using Visual Studio 2005 Report Viewer control.

share this article :
post comment
 

Mahesh, Thanks for the awesome post, it helps a lot, i wanted to ask you if i can bind rdlc report from xml in MVC2.0, if so, can you please send me any example to look into it..thanks

Posted by Vishal Shyam Feb 21, 2012

Yes

Posted by Mahesh Chand Apr 07, 2009

But I saw your another article "Build Reports from a DataSet using ReportViewer".

At step 4: "Fill Data". The code creates a DataSet, loads the data from Data.xml file and

sets EmployeeBindingSource. DataSource as DataSet.

My question is in this article since the report is from  Database, so step 4 can be ignored

because it is regarding a xml file. 

Posted by Hui Zhao Oct 22, 2008

This article shows how to display data in a report from an XML data file. Data is not coming from the database. But similar approach can be used if data is coming from the database. Look at my other articles in the same section.

Posted by Mahesh Chand Oct 22, 2008

Thanks Mahesh. I have a little confusion. The data source is from a database or a xml file? From figure 3, it looks like come from a datatable. But since the data can be extracted from the database, why data.xml is requred? You mean dynamic rdlc? Thanks Hui

Posted by Hui Zhao Oct 22, 2008
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor