Articles in this section
Category / Section

Changing the XML DataSource Dynamically at Runtime in Bold Reports

Published:
Updated:

Bold Reports is a powerful reporting tool that enables you to create and render reports from a wide range of data sources, including XML. In scenarios where the XML data source needs to be updated dynamically at runtime—such as in server-side applications like .NET Core, .NET MVC, or Blazor—Bold Reports provides the flexibility to handle this efficiently.

You can achieve this by deserializing the XML data (read as a text file) and loading it into the report during runtime using the OnInitReportOptions method.

This article will guide you through the steps to dynamically change the XML data source at runtime in a Blazor Report Viewer application using Bold Reports.

Modifying Report Details using ReportSerializer

The ReportSerializer class in Bold Reports allows you to modify report definitions at runtime. It is used to serialize and deserialize reports in RDL (Report Definition Language) format, enabling dynamic customization of report properties.

One key use case is adjusting the report’s data source connection dynamically to point to different XML sources during runtime.

Example Code

The following code snippet demonstrates how to change the XML data source for a report at runtime using the ReportSerializer class:

public void OnInitReportOptions(ReportViewerOptions reportOption)
{
    // Get the root path of the web application.
    string basePath = _hostingEnvironment.WebRootPath;
    // Load the XmlReport.rdl report from the application folder wwwroot\Resources.
    System.IO.FileStream inputStream = new System.IO.FileStream(basePath + @"\Resources\" + reportOption.ReportModel.ReportPath + ".rdl", System.IO.FileMode.Open, System.IO.FileAccess.Read);
    MemoryStream reportStream = new MemoryStream();
    inputStream.CopyTo(reportStream);
    reportStream.Position = 0;
    inputStream.Close();

    // Instantiate a serializer to deserialize the report definition.
    BoldReports.RDL.DOM.ReportSerializer serializer = new BoldReports.RDL.DOM.ReportSerializer();
    BoldReports.RDL.DOM.ReportDefinition reportDefinition = serializer.GetReportDefinition(reportStream);

    // Load the XML data from the NewData.xml file into an XmlDocument.
    var xmlDoc = new System.Xml.XmlDocument();
    xmlDoc.Load(basePath + @"\Resources\NewData.xml");
    var xmlDocString = xmlDoc.InnerXml;
    var xmlDocByteArray = Encoding.ASCII.GetBytes(xmlDocString);
    var xmlDocBase64 = Convert.ToBase64String(xmlDocByteArray);
    // Update the report's data source with the Base64 encoded XML data.
    // You can find the data source and then you can change datasource based on your requirements.
    reportDefinition.DataSources[0].ConnectionProperties.EmbeddedData.Data = xmlDocBase64;

    MemoryStream stream = new MemoryStream();
    serializer.SaveReportDefinition(stream, reportDefinition);
    stream.Position = 0;
    reportOption.ReportModel.Stream = stream;    
}

Run the Application

After implementing the code, build and run your application. The report will render using the XML data from the NewData.xml file.

Actual report record snapshot:

ActualReportRecordOutput.png

Modified at runtime snapshot:

XMLDataOutput.png

By following the outlined steps and using the provided code example, you can dynamically change the XML data source for your reports in Bold Reports at runtime. This approach offers enhanced flexibility and customization, allowing reports to adapt to changing data requirements.

You can download the sample application from here.

See Also

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied