[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:

<Contacts>
 <Contact>
 <ID>1</ID>
 <Name>John Doe</Name>
 <Age>23</Age>
 <Sex>Male</Sex>
 <Email>johndoe@gmail.com</Email>
 <Phone>703-865-3356</Phone>
 </Contact>
    ...
</Contacts>

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.

public class Contact
{
 public int ContactID { get; set; }
 public string Name { get; set; }
 public int Age { get; set; }
 public string Sex { get; set; }
 public string Email { get; set; }
 public string Phone { get; set; }
}

Once the project is set up, you’re ready to add your first report and connect to the data. solution_explorer

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.

report_events 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.

private void Contacts_NeedDataSource(object sender, EventArgs e)
{
 // Read the embedded XML file
    var assembly = Assembly.GetExecutingAssembly();
    var stream = assembly.GetManifestResourceStream("Telerik.Examples.Data.contacts.xml");
    var xml = XDocument.Load(new StreamReader(stream));
 
 // Query the XML data
    var contacts = from c in xml.Descendants("Contact")
                   select new Contact
                   {
                       ContactID = Convert.ToInt32(c.Element("ID").Value),
                       Name = c.Element("Name").Value,
                       Age = Convert.ToInt32(c.Element("Age").Value),
                       Sex = c.Element("Sex").Value,
                       Email = c.Element("Email").Value,
                       Phone = c.Element("Phone").Value,
                   };
    (sender as Telerik.Reporting.Processing.Report).DataSource = contacts.ToList<Contact>();
}

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.

report_wizard

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.

choose_datasource On the following screen, you’ll be prompted to select the business object you want to bind to. Drill down in ReportLibrary to the type you want to use, in this case Contact, and click Next.

business_object On the following screen select the Standard report type and hit Next. To design your data layout, choose all of the items you want to display and add them to the Detail section.

data_layout 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:

report_layout

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.

datasource_property

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.

report Conclusion

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:

[Source: C#] [Source: VB]


Related Posts

Comments

Comments are disabled in preview mode.