[OBSOLETE. Instead use the approach suggested in Does Telerik Reporting support XML as a datasource?]
One of the cool things about Telerik Reporting is that you can bind it to just about any kind of data source. Regardless of whether your data is stored in a SQL Server database, an XML document, or a simple text file, if you can read it into one of the supported data source types then you can bind it to your report. Since XML is a often used to store data, I thought it might be helpful to demonstrate how to bind your report to the data in an XML file using LINQ to XML.
To get started, create a class library to hold your reports called ReportLibrary, and then a WinForms client and ASP.NET web application to display the reports. You don’t really have to create both client and web applications to display your report, but I like to see how the report looks on both. Next, add a folder named Data to the project and drag in the XML file which contains all the data. Here’s what the XML file for this example looks like:
Click on the XML file and then go to your properties window. Under the Build Action drop down list, choose the Embedded Resource option. Finally, add a custom entity class to the Entities folder which will act as a model for the contact data stored in the XML file.
To add a new Telerik Report to the report library, right-click on the project and select Add | New Item… Select the Reporting menu and choose a Telerik Report. Give the report a name (in this case, Contacts.cs), and select Add. A wizard will automatically appear, prompting you to set up your report. Go ahead and exit the wizard as you’ll return to it shortly. First you need to connect the report to your XML data. To do this, select your report by clicking anywhere on the report designer surface or on the report selector in the top, left-hand corner of the designer. In your properties window, click the orange lightning bolt at the top to select the report events. Double-click in the NeedDataSource event textbox, which will add an event handler to your code-behind.
In your new event handler, you can add code to connect to the XML file and parse it using LINQ to XML. The following code does just that, reading the embedded XML file into an XDocument and then parsing it into an IEnumerable collection of Contact objects. Finally, the collection is converting into a List<Contact> and assigned to the report’s DataSource property.
Binding the report to XML is as simple as that. Now that the hard part is over, simply head back to your report, re-launch the wizard, and design the report layout. To launch the wizard from the designer, click on Telerik Reporting | Report Wizard from the Visual Studio menu.
You’ll be greeted with a welcome screen, to which you can click Next. On the Report Choice page, select New Report and click Next. On the Choose Data Source page, select New Data Source and click Next. Now you’ll be asked to choose a data source type. Make sure you select the Business Object option, and then click Next.
On the next screen choose a Stepped layout and ensure that the Adjust report items to fit available space checkbox is selected. Click Next. Choose a report style on the next screen (I like the Corporate style), click Next, then click Finish. Your report should look like this:
One important thing to note is that since you are using the NeedDataSource event, you must go into the report’s properties window and remove its DataSource. This property was set by the wizard and can now be removed since the wizard has generated our report and bound the data fields. The reason you must do this is because the NeedDataSource event is only fired if no data source has already been set. If you forget to do this, you might be left scratching your head when you run the report and it contains no data.
Now all that’s left is to add a ReportViewer control to your ASP.NET or WinForms project, add this report to the viewer, and run your project. You should see the report display your XML data just fine.
There are quite a few different places we store data these days. With Telerik Reporting, it’s easy to bind to data from any source (including XML!) as long as the data can be read into one of the supported types. If you have questions about how to read your data into one of these formats, drop a line in the Reporting Forums or a comment here and we’ll get you squared away.
This was my first post on the use of Telerik’s Reporting tool. You can expect to see more tips in the future, but I’m interested in hearing what features you want to learn about. Drop me a comment if there’s a topic you’d like me to cover in subsequent posts or RadTips episodes.
If you’re interested in the source code for this example, you can find it here:
Subscribe to be the first to get our expert-written articles and tutorials for developers!