KB Article T269534
Visible to All Users

How to serialize a report to XML with an untyped DataSet as a data source

Description:
I have a report with an untyped data set as a data source. I serialize the report to XML and then restore it but the data source is empty. How to resolve this?

Answer:
The following data sources provide built-in serialization:

  1. SqlDataSource
  2. EFDataSource
  3. ObjectDataSource
  4. JsonDataSource
  5. XPObjectSource

Since the Web Report Designer works with the XML report representation, incorrect results may be produced after you load a report which uses an untyped data set as a data source.

We need to serialize an untyped data source to resolve the issue.
To do so, implement a serializer which implements the DevExpress.XtraReports.Native.IDataSerializer interface and use the XML-serialization capabilities exposed by the DataSet class to serialize/deserialize this data set as follows.

In the IDataSerializer.Serialize method, serialize a DataSet into XML through the DataSet.WriteXml (or DataSet.GetXml) method.
In the IDataSerializer.Deserialize method, create a new DataSet instance and populate it from a properly formatted XML stream by using the DataSet.ReadXml method.

Here is sample code.

C#
public class CustomUntypedDataSetSerializer : IDataSerializer { public const string Name = "CustomUntypedDataSetSerializer"; public bool CanSerialize(object data, object extensionProvider) { return (data is DataSet); } public string Serialize(object data, object extensionProvider) { if (data is DataSet) { DataSet ds = data as DataSet; System.Text.StringBuilder sb = new System.Text.StringBuilder(); XmlWriter writer = XmlWriter.Create(sb); ds.WriteXml(writer, XmlWriteMode.WriteSchema); return sb.ToString(); } return string.Empty; } public bool CanDeserialize(string value, string typeName, object extensionProvider) { return typeName == "System.Data.DataSet"; } public object Deserialize(string value, string typeName, object extensionProvider) { DataSet ds = new DataSet(); using (XmlReader reader = XmlReader.Create(new StringReader(value))) { ds.ReadXml(reader, XmlReadMode.ReadSchema); } return ds; } }

Initialize the custom serializer using the SerializationService.RegisterSerializer method at the application start.

C#
//in the static form constructor static Form1() { SerializationService.RegisterSerializer(CustomUntypedDataSetSerializer.Name, new CustomUntypedDataSetSerializer()); } //when initializing a report void InitReport(){ //... report.Extensions[SerializationService.Guid] = CustomUntypedDataSetSerializer.Name; //... }

This solution is illustrated by the following examples.
How to serialize a report with an untyped data set as a data source (for the ASP.NET platform)
How to serialize a report to XML with an untyped data set as a data source (for the WinForms platform).

See also:
WinForms: How to implement custom XML serialization of a report that is bound to a dataset
Web: Web Report Designer - How to use the custom serializer class (IDataSerializer) to serialize the XPO data source to make it visible in the web report designer

Show previous comments (7)
Yaroslav (DevExpress Support) 7 years ago

    Hi Gábor,
    The T599429  ticket is private, and that's why you can't see it. In any case, it may appear that your scenario is different that the one described there. Let's discuss your particular use case in a separate thread I've created on my behalf: XtraReport - a custom DataSet serializer is slow for a large number of rows.

    AK AK
    Artyom Kamenschikov 5 years ago

      Is it possible to serialize a DataSet for Angular report viewer? Or it won't be able to deserialize it back because custom deserializer method exists only in .net part?

      Vasily (DevExpress Support) 5 years ago

        Hi Artyom,

        The server-side part of our Reporting components always uses .NET, so the information provided in this article is relevant for Angular applications. The serialization and deserialization are always performed in the server-side code.
        If you face any difficulties using this approach in your Angular project, feel free to submit a separate ticket and describe the issue in greater detail. This way, we will be able to research it and provide you with our recommendations on how to resolve it.

        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.