Progress® Telerik® Reporting R3 2017

Serialize Report Definition in XML

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.

Caution note Caution

The following content is legacy. In order to better handle report resources we provide a ReportPackager that serializes the report definition in XML and with a zip compression packages the definition and its resources. This is the default report document format for the Report Designers. For more information see: Package Report Definition.

Report Definition

The XML serialization/deserialization of report definitions is achieved through the dedicated ReportXmlSerializer class. To illustrate how a report is serialized and deserialized, let us start with a simple dynamically generated report definition:

var report = new Report1();

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

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

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

var 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);

var 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);

var 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;

Serialize Report Definition to an XML file

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

using (System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create("SerializedReport.xml"))
{
    Telerik.Reporting.XmlSerialization.ReportXmlSerializer xmlSerializer =
        new Telerik.Reporting.XmlSerialization.ReportXmlSerializer();

    xmlSerializer.Serialize(xmlWriter, report);
}

Deserialize Report Definition from XML file

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

System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.IgnoreWhitespace = true;

using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create("Report1.xml", settings))
{
    Telerik.Reporting.XmlSerialization.ReportXmlSerializer xmlSerializer =
        new Telerik.Reporting.XmlSerialization.ReportXmlSerializer();

    Telerik.Reporting.Report report = (Telerik.Reporting.Report)
        xmlSerializer.Deserialize(xmlReader);
}

Serialized Report Definition in XML

and the resulting XML file would look like this:

<Report DataSourceName="sqlDataSource1" Width="4in" Name="userReport1" xmlns="http://schemas.telerik.com/reporting/2012/2">
  <DataSources>
    <SqlDataSource ConnectionString="Data Source=.\SqlExpress;Initial Catalog=AdventureWorks;Integrated Security=True" SelectCommand="select ProductNumber, Name from Production.Product" Name="sqlDataSource1" />
  </DataSources>
  <Items>
    <DetailSection Height="0.2in" Name="detailSection1">
      <Items>
        <TextBox Value="=Fields.ProductNumber" Size="2in, 0.2in" Location="0in, 0cm" Name="textBox1" />
        <TextBox Value="=Fields.Name" Size="2in, 0.2in" Location="2in, 0cm" Name="textBox2" />
      </Items>
    </DetailSection>
  </Items>
  <PageSettings>
    <PageSettings PaperKind="Letter" Landscape="False">
      <Margins>
        <MarginsU Left="1in" Right="1in" Top="1in" Bottom="1in" />
      </Margins>
    </PageSettings>
  </PageSettings>
</Report>