Search
XML Serialization

Reports can be serialized to and deserialized from XML files through one of the SaveToXml and LoadFromXml overloads respectively.

Serializing Data Sources

Data sources that are referenced by various report elements are not serialized/deserialized automatically when a report is saved or loaded from a file. When a report is saved to a file, the SerializeDataSource event is raised for each data source in the report. The event argument provides a reference to the data source being serialized and expects to receive a name that should identify the data source in the XML file. This event can be raised multiple times for the same data source if a data source is referenced by more than one report item. When a data source is deserialized, the DeserializeDataSource event is raised to allow clients to resolve a data source using the name specified during serialization.

For example, when serializing a DataSet data source, the DataSetName can be used as a name.

Custom Report Serialization

Internally the SaveToXml and LoadFromXml methods call the Save and Load protected methods of the Report class to carry out the serialization and deserialization of the report contents. These methods can be overriden in derived report classes to serialize and deserialize custom report contents. For example, the following Report-derived class provides a new PageMargins property and overrides Save and Load in order to handle the serialization/deserialization of this property:

C#  Copy Code

class MyReport : Report
{
    protected override void Save(XmlElement element, XmlSerializationContext context)
    {
        context.WriteMargins(PageMargins, "pageMargins", element);
        base.Save(element, context);
    }

    protected override void Load(XmlElement element, XmlSerializationContext context)
    {
        PageMargins = context.ReadMargins("pageMargins", element);
        base.Load(element, context);
    }

    public Margins PageMargins
    {
        get { return pageMargins; }
        set { pageMargins = value; }
    }

    private Margins pageMargins;
}

Visual Basic  Copy Code

Class MyReport
    Inherits Report

    Protected Overrides Sub Save(element As XmlElement, context As XmlSerializationContext)
        context.WriteMargins(PageMargins, "pageMargins", element)
        MyBase.Save(element, context)
    End Sub

    Protected Overrides Sub Load(element As XmlElement, context As XmlSerializationContext)
        PageMargins = context.ReadMargins("pageMargins", element)
        MyBase.Load(element, context)
    End Sub

    Public Property PageMargins() As Margins
        Get
            Return _pageMargins
        End Get
        Set
            _pageMargins = value
        End Set
    End Property

    Private _pageMargins As Margins

End Class