Example E3157
Visible to All Users

Reporting for WinForms - How to Load and Restore a Report Using Custom XML Serialization

This example demonstrates the capability to serialize a report and its data source to XML.

Custom report serialization is implemented in the ReportStorageExtension class. The ReportDesignExtension class implements custom data source serialization.

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

WindowsApplication54/Form1.cs(vb)
C#
using System; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Windows.Forms; using DevExpress.XtraReports; using DevExpress.XtraReports.Extensions; using DevExpress.XtraReports.UI; // ... namespace WindowsApplication54 { public partial class Form1 : Form { static Form1() { // The following code is required to support serialization of multiple custom objects. TypeDescriptor.AddAttributes(typeof(DataSet), new ReportAssociatedComponentAttribute()); TypeDescriptor.AddAttributes(typeof(OleDbDataAdapter), new ReportAssociatedComponentAttribute()); // The following code is required to serialize custom objects. ReportExtension.RegisterExtensionGlobal(new ReportExtension()); ReportDesignExtension.RegisterExtension(new DesignExtension(), ExtensionName); } private const string ExtensionName = "Custom"; public Form1() { InitializeComponent(); } private void createReportWhithDataSourceButton_Click(object sender, EventArgs e) { using(XtraReport report = new XtraReport()) { using(ReportDesignTool tool = new ReportDesignTool(report)) { tool.DesignForm.DesignMdiController.DesignPanelLoaded += OnDesignPanelLoaded; tool.ShowDesignerDialog(); } } } void OnDesignPanelLoaded(object sender, DevExpress.XtraReports.UserDesigner.DesignerLoadedEventArgs e) { ReportDesignExtension.AssociateReportWithExtension((XtraReport)e.DesignerHost.RootComponent, ExtensionName); } private void loadReportfromFileButton_Click(object sender, EventArgs e) { OpenFileDialog openfd = new OpenFileDialog(); if (openfd.ShowDialog() != DialogResult.OK) return; XtraReport report = new XtraReport(); report.LoadLayoutFromXml(openfd.FileName); report.ShowDesignerDialog(); } } }
WindowsApplication54/ReportExtension.cs(vb)
C#
using System.IO; using DevExpress.XtraReports.Extensions; using DevExpress.XtraReports.UI; class ReportExtension : ReportStorageExtension { public override void SetData(XtraReport report, Stream stream) { report.SaveLayoutToXml(stream); } }
WindowsApplication54/DesignExtension.cs(vb)
C#
using System; using System.Data; using System.Data.OleDb; using System.IO; using DevExpress.XtraReports.Extensions; using DevExpress.XtraReports.UI; class DesignExtension : ReportDesignExtension { protected override bool CanSerialize(object data) { return data is DataSet || data is OleDbDataAdapter; } protected override string SerializeData(object data, XtraReport report) { if (data is DataSet) return (data as DataSet).GetXmlSchema(); if (data is OleDbDataAdapter) { OleDbDataAdapter adapter = data as OleDbDataAdapter; return adapter.SelectCommand.Connection.ConnectionString + "\r\n" + adapter.SelectCommand.CommandText; } return base.SerializeData(data, report); } protected override bool CanDeserialize(string value, string typeName) { return typeof(DataSet).FullName == typeName || typeof(OleDbDataAdapter).FullName == typeName; } protected override object DeserializeData(string value, string typeName, XtraReport report) { if (typeof(DataSet).FullName == typeName) { DataSet dataSet = new DataSet(); dataSet.ReadXmlSchema(new StringReader(value)); return dataSet; } if (typeof(OleDbDataAdapter).FullName == typeName) { OleDbDataAdapter adapter = new OleDbDataAdapter(); string[] values = value.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); adapter.SelectCommand = new OleDbCommand(values[1], new OleDbConnection(values[0])); return adapter; } return base.DeserializeData(value, typeName, report); } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.