Often times, you’ll need to store data from your application in XML files. Well, what if you want to report on this data… it’s not in a database, so how do you do it? I’m happy to say that you can easily report on XML data through the use of the ObjectDataSource component provided with Telerik Reporting. In this blog entry, we’ll take a look at how this can be accomplished.
For this example, I’ve generated some XML data in advance using this helpful data generation website.
<
Accounts
>
<
Account
>
<
Name
>Cody Whitfield</
Name
>
<
Email
>ut.pharetra@adipiscingligulaAenean.ca</
Email
>
</
Account
>
<
Account
>
<
Name
>Stewart Douglas</
Name
>
<
Email
>Aliquam.vulputate.ullamcorper@etultrices.ca</
Email
>
</
Account
>
<
Account
>
<
Name
>Cooper Deleon</
Name
>
<
Email
>vitae@cubiliaCurae;.ca</
Email
>
</
Account
>
</
Accounts
>
In order to make this data usable with Telerik Reporting, you’ll need to start by creating a typed DataSet. Doing so will allow you to easily load your XML data, and since it will be typed, Telerik Reporting will be able to pick up the available column names for use with the expression builder and data explorer.
Generating the DataSet is actually a pretty simple process. Visual Studio includes a tool for generating XML Schemas and their corresponding DataSets. In order to access this tool, you will need to start an instance of the Visual Studio Command Prompt. The shortcut to start this command prompt should be located under the Visual Studio folder in the Start menu. Once in the command prompt, you can generate an XSD and DataSet using the following command.
"xsd AccountData.xml /d"
This will automatically generate an XML Schema and DataSet that you can include in your reporting project. In order to actually access it from a Telerik Report, you’ll need to take an additional step and wrap it with a custom repository class containing data access methods. This is what you’ll bind the ObjectDataSource component to. The following code accomplishes this by providing a method that retrieves all available data.
public
class
AccountDataRepository
{
public
ReportLibrary.Accounts.AccountDataTable GetAccounts()
{
Accounts dataset =
new
Accounts();
// TODO: Adjust this to point to the location of your XML data
dataset.ReadXml(
"C:\\Source\\Data\\AccountData.xml"
);
return
dataset.Account;
}
}
Once you have a DataSet and a Repository class, creating the Telerik Report is the easy part. All you need to do is create a new report, and run through the report wizard.
In order to bind to the custom Repository class, you’ll need to use an ObjectDataSource component.
The Business Object you will point to will be the type of the repository class. In this case, our repository class is call AccountDataRepository.
You will also need to select a method of the repository class that returns data. For this example, the GetAccounts() method of our repository will return the necessary data from our XML file.
Once the ObjectDataSource has been created, Telerik Reporting will detect the available fields. Completing the report is simply a matter of running through the rest of the steps in the Report Wizard.
The completed report looks something like this...
Click here to download the source.