Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Mindcracker MVP Summit 2012
Search :       Advanced Search »
Home » Reporting Services » Building Reports from XML Documents using ReportViewer

Building Reports from XML Documents using ReportViewer

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.

Author Rank :
Page Views : 11157
Downloads : 218
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ReportFromXmlDoc.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
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.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
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!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Data Sources by Hui On October 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
Reply | Email | Modify 
Re: Data Sources by Mahesh On October 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.
Reply | Email | Modify 
Re: Re: Data Sources by Hui On October 22, 2008

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. 

Reply | Email | Modify 
Re: Re: Re: Data Sources by Mahesh On April 7, 2009
Yes
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.