Telerik Reporting

Telerik Reporting supports serialization/deserialization of the report definition as XML. This is useful in various different scenarios and opens many possibilities that are not easily accomplished otherwise. For example, this allows adding or modifying reports in your application without recompiling or redeploying it. Another typical scenario is saving/loading of dynamically generated report definitions or transferring them over the network.

Report Definition

The XML serialization/deserialization of report definitions employs the build-in mechanism for XML serialization of the .NET Framework, and more specifically the System.Xml.Serialization.XmlSerializer class. To illustrate this, let us start with a simple dynamically generated report definition:

CopyC#
Telerik.Reporting.Report report = new Telerik.Reporting.Report();

report.Width = Telerik.Reporting.Drawing.Unit.Inch(4);

Telerik.Reporting.DetailSection detailSection = new Telerik.Reporting.DetailSection();

detailSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.2);
report.Items.Add(detailSection);

Telerik.Reporting.TextBox numberTextBox = new Telerik.Reporting.TextBox();

numberTextBox.Value = "=Fields.ProductNumber";
numberTextBox.Left = Telerik.Reporting.Drawing.Unit.Inch(0);
numberTextBox.Top = Telerik.Reporting.Drawing.Unit.Inch(0);
numberTextBox.Width = Telerik.Reporting.Drawing.Unit.Inch(2);
numberTextBox.Height = Telerik.Reporting.Drawing.Unit.Inch(0.2);
detailSection.Items.Add(numberTextBox);

Telerik.Reporting.TextBox nameTextBox = new Telerik.Reporting.TextBox();

nameTextBox.Value = "=Fields.Name";
nameTextBox.Left = Telerik.Reporting.Drawing.Unit.Inch(2);
nameTextBox.Top = Telerik.Reporting.Drawing.Unit.Inch(0);
nameTextBox.Width = Telerik.Reporting.Drawing.Unit.Inch(2);
nameTextBox.Height = Telerik.Reporting.Drawing.Unit.Inch(0.2);
detailSection.Items.Add(nameTextBox);

Telerik.Reporting.SqlDataSource dataSource = new Telerik.Reporting.SqlDataSource();

dataSource.ConnectionString = "Data Source=.\\SqlExpress;Initial Catalog=AdventureWorks;Integrated Security=True";
dataSource.SelectCommand = "select ProductNumber, Name from Production.Product";
report.DataSource = dataSource;
CopyVB.NET
Dim report As New Telerik.Reporting.Report()

report.Width = Telerik.Reporting.Drawing.Unit.Inch(4)

Dim detailSection As New Telerik.Reporting.DetailSection()

detailSection.Height = Telerik.Reporting.Drawing.Unit.Inch(0.2)
report.Items.Add(detailSection)

Dim numberTextBox As New Telerik.Reporting.TextBox()

numberTextBox.Value = "=Fields.ProductNumber"
numberTextBox.Left = Telerik.Reporting.Drawing.Unit.Inch(0)
numberTextBox.Top = Telerik.Reporting.Drawing.Unit.Inch(0)
numberTextBox.Width = Telerik.Reporting.Drawing.Unit.Inch(2)
numberTextBox.Height = Telerik.Reporting.Drawing.Unit.Inch(0.2)
detailSection.Items.Add(numberTextBox)

Dim nameTextBox As New Telerik.Reporting.TextBox()

nameTextBox.Value = "=Fields.Name"
nameTextBox.Left = Telerik.Reporting.Drawing.Unit.Inch(2)
nameTextBox.Top = Telerik.Reporting.Drawing.Unit.Inch(0)
nameTextBox.Width = Telerik.Reporting.Drawing.Unit.Inch(2)
nameTextBox.Height = Telerik.Reporting.Drawing.Unit.Inch(0.2)
detailSection.Items.Add(nameTextBox)

Dim dataSource As New Telerik.Reporting.SqlDataSource()

dataSource.ConnectionString = "Data Source=.\SqlExpress;Initial Catalog=AdventureWorks;Integrated Security=True"
dataSource.SelectCommand = "select ProductNumber, Name from Production.Product"
report.DataSource = dataSource

Serialize Report Definition to an XML file

The following sample code snipped demonstrates how to serialize the above report definition to an XML file:

CopyC#
using (System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create("report.xml"))
{
    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Telerik.Reporting.Report));

    xmlSerializer.Serialize(xmlWriter, report);
}
CopyVB.NET
Using xmlWriter As System.Xml.XmlWriter = System.Xml.XmlWriter.Create("report.xml")
    Dim xmlSerializer As New System.Xml.Serialization.XmlSerializer(GetType(Telerik.Reporting.Report))

    xmlSerializer.Serialize(xmlWriter, report)
End Using

Deserialize Report Definition from XML file

The corresponding code that can be used to deserialize the report definition from the file is shown below:

CopyC#
using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create("report.xml"))
{
    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Telerik.Reporting.Report));

    Telerik.Reporting.Report report = (Telerik.Reporting.Report)xmlSerializer.Deserialize(xmlReader);
}
CopyVB.NET
Using xmlReader As System.Xml.XmlReader = System.Xml.XmlReader.Create("report.xml")
    Dim xmlSerializer As New System.Xml.Serialization.XmlSerializer(GetType(Telerik.Reporting.Report))

    Dim resultReport As Telerik.Reporting.Report = DirectCast(xmlSerializer.Deserialize(xmlReader), Telerik.Reporting.Report)
End Using

Serialized Report Definition in XML

and the resulting XML file would look like this:

CopyXML
<?xml version="1.0" encoding="utf-8"?>
<Report Width="4in">
  <PageSettings PaperKind="Letter" Landscape="False">
    <Margins Left="1in" Right="1in" Top="1in" Bottom="1in" />
  </PageSettings>
  <DataSource>
    <SqlDataSource ConnectionString="Data Source=.\SqlExpress;Initial Catalog=AdventureWorks;Integrated Security=True"
                   SelectCommand="select ProductNumber, Name from Production.Product" />
  </DataSource>
  <Items>
    <DetailSection Height="0.2in">
      <Items>
        <TextBox Value="=Fields.ProductNumber" Left="0in" Top="0in" Width="2in" Height="0.2in" />
        <TextBox Value="=Fields.Name" Left="2in" Top="0in" Width="2in" Height="0.2in" />
      </Items>
    </DetailSection>
  </Items>
</Report>