Edit this page

Serialize XmlaDataProvider

In this article we will show you how to serialize and deserialize XmlaDataProvider and all of its settings. You can use this feature to save the current state of the provider and load it next time when the application is started.

Serialize XmlaDataProvider with DataContractSerializer

We've added the DataContract attribute to all classes used by XmlaDataProvider. So you can easily serialize it by using DataContractSerializer. Below you will find out how to create a serializer and use it with RadPivotGrid.

Define Members


[DataContract]
public class DataProviderSettings
{
    [DataMember]
    public object[] Aggregates { get; set; }

    [DataMember]
    public object[] Filters { get; set; }

    [DataMember]
    public object[] Rows { get; set; }

    [DataMember]
    public object[] Columns { get; set; }

    [DataMember]
    public int AggregatesLevel { get; set; }
    [DataMember]
    public PivotAxis AggregatesPosition { get; set; }
}

<DataContract> _
Public Class DataProviderSettings
    <DataMember> _
    Public Property Aggregates() As Object()
        Get
            Return m_Aggregates
        End Get
        Set(value As Object())
            m_Aggregates = value
        End Set
    End Property
    Private m_Aggregates As Object()
    <DataMember> _
    Public Property Filters() As Object()
        Get
            Return m_Filters
        End Get
        Set(value As Object())
            m_Filters = value
        End Set
    End Property
    Private m_Filters As Object()
    <DataMember> _
    Public Property Rows() As Object()
        Get
            Return m_Rows
        End Get
        Set(value As Object())
            m_Rows = value
        End Set
    End Property
    Private m_Rows As Object()
    <DataMember> _
    Public Property Columns() As Object()
        Get
            Return m_Columns
        End Get
        Set(value As Object())
            m_Columns = value
        End Set
    End Property
    Private m_Columns As Object()
    <DataMember> _
    Public Property AggregatesLevel() As Integer
        Get
            Return m_AggregatesLevel
        End Get
        Set(value As Integer)
            m_AggregatesLevel = value
        End Set
    End Property
    Private m_AggregatesLevel As Integer
    <DataMember> _
    Public Property AggregatesPosition() As PivotAxis
        Get
            Return m_AggregatesPosition
        End Get
        Set(value As PivotAxis)
            m_AggregatesPosition = value
        End Set
    End Property
    Private m_AggregatesPosition As PivotAxis
End Class

The next step is to implement the serializer. When serializing the provider, you have to create an instance of the DataProviderSettings class and set all of the properties. After that you can serialize the instance to a file or a stream. When using DataContractSerializer you have to pass a collection of KnownTypes to the serializer. That's why we've created a new XmlaPivotSerializationHelper class which has a static member - KnownTypes. It consits of all types you'll need in order to serialize XmlaDataProvider.

Data Provider Implementation


public abstract class DataProviderSerializer
{
    public abstract IEnumerable<Type> KnownTypes { get; }

    public string Serialize(object context)
    {
        string serialized = string.Empty;
        IDataProvider dataProvider = context as IDataProvider;
        if (dataProvider != null)
        {
            MemoryStream stream = new MemoryStream();

            DataProviderSettings settings = new DataProviderSettings()
            {
                Aggregates = dataProvider.Settings.AggregateDescriptions.OfType<object>().ToArray(),
                Filters = dataProvider.Settings.FilterDescriptions.OfType<object>().ToArray(),
                Rows = dataProvider.Settings.RowGroupDescriptions.OfType<object>().ToArray(),
                Columns = dataProvider.Settings.ColumnGroupDescriptions.OfType<object>().ToArray(),
                AggregatesLevel = dataProvider.Settings.AggregatesLevel,
                AggregatesPosition = dataProvider.Settings.AggregatesPosition
            };

            DataContractSerializer serializer = new DataContractSerializer(typeof(DataProviderSettings), KnownTypes);
            serializer.WriteObject(stream, settings);
            stream.Position = 0;
            var streamReader = new StreamReader(stream);
            serialized += streamReader.ReadToEnd();
        }

        return serialized;
    }

    public void Deserialize(object context, string savedValue)
    {
        IDataProvider dataProvider = context as IDataProvider;
        if (dataProvider != null)
        {
            var stream = new MemoryStream();
            var tw = new StreamWriter(stream);
            tw.Write(savedValue);
            tw.Flush();
            stream.Position = 0;

            DataContractSerializer serializer = new DataContractSerializer(typeof(DataProviderSettings), KnownTypes);
            var result = serializer.ReadObject(stream);

            dataProvider.Settings.AggregateDescriptions.Clear();
            foreach (var aggregateDescription in (result as DataProviderSettings).Aggregates)
            {
                dataProvider.Settings.AggregateDescriptions.Add(aggregateDescription);
            }

            dataProvider.Settings.FilterDescriptions.Clear();
            foreach (var filterDescription in (result as DataProviderSettings).Filters)
            {
                dataProvider.Settings.FilterDescriptions.Add(filterDescription);
            }

            dataProvider.Settings.RowGroupDescriptions.Clear();
            foreach (var rowDescription in (result as DataProviderSettings).Rows)
            {
                dataProvider.Settings.RowGroupDescriptions.Add(rowDescription);
            }

            dataProvider.Settings.ColumnGroupDescriptions.Clear();
            foreach (var columnDescription in (result as DataProviderSettings).Columns)
            {
                dataProvider.Settings.ColumnGroupDescriptions.Add(columnDescription);
            }

            dataProvider.Settings.AggregatesPosition = (result as DataProviderSettings).AggregatesPosition;
            dataProvider.Settings.AggregatesLevel = (result as DataProviderSettings).AggregatesLevel;
        }
    }
}

Public MustInherit Class DataProviderSerializer
    Public MustOverride ReadOnly Property KnownTypes() As IEnumerable(Of Type)
    Public Function Serialize(context As Object) As String
        Dim serialized As String = String.Empty
        Dim dataProvider As IDataProvider = TryCast(context, IDataProvider)
        If dataProvider IsNot Nothing Then
            Dim stream As New MemoryStream()
            Dim settings As New DataProviderSettings() With {
                 .Aggregates = dataProvider.Settings.AggregateDescriptions.OfType(Of Object)().ToArray(),
                 .Filters = dataProvider.Settings.FilterDescriptions.OfType(Of Object)().ToArray(),
                 .Rows = dataProvider.Settings.RowGroupDescriptions.OfType(Of Object)().ToArray(),
                 .Columns = dataProvider.Settings.ColumnGroupDescriptions.OfType(Of Object)().ToArray(),
                 .AggregatesLevel = dataProvider.Settings.AggregatesLevel,
                 .AggregatesPosition = dataProvider.Settings.AggregatesPosition
            }
            Dim serializer As New DataContractSerializer(GetType(DataProviderSettings), KnownTypes)
            serializer.WriteObject(stream, settings)
            stream.Position = 0
            Dim streamReader = New StreamReader(stream)
            serialized += streamReader.ReadToEnd()
        End If
        Return serialized
    End Function
    Public Sub Deserialize(context As Object, savedValue As String)
        Dim dataProvider As IDataProvider = TryCast(context, IDataProvider)
        If dataProvider IsNot Nothing Then
            Dim stream = New MemoryStream()
            Dim tw = New StreamWriter(stream)
            tw.Write(savedValue)
            tw.Flush()
            stream.Position = 0
            Dim serializer As New DataContractSerializer(GetType(DataProviderSettings), KnownTypes)
            Dim result = serializer.ReadObject(stream)
            dataProvider.Settings.AggregateDescriptions.Clear()
            For Each aggregateDescription As AggregateDescriptionBase In TryCast(result, DataProviderSettings).Aggregates
                dataProvider.Settings.AggregateDescriptions.Add(aggregateDescription)
            Next
            dataProvider.Settings.FilterDescriptions.Clear()
            For Each filterDescription As FilterDescription In TryCast(result, DataProviderSettings).Filters
                dataProvider.Settings.FilterDescriptions.Add(filterDescription)
            Next
            dataProvider.Settings.RowGroupDescriptions.Clear()
            Dim rows = TryCast(result, DataProviderSettings).Rows
            For index = 0 To rows.Count - 1
                dataProvider.Settings.RowGroupDescriptions.Add(rows(index))
            Next
            dataProvider.Settings.ColumnGroupDescriptions.Clear()
            Dim columns = TryCast(result, DataProviderSettings).Columns
            For index = 0 To columns.Count - 1
                dataProvider.Settings.ColumnGroupDescriptions.Add(columns(index))
            Next
            dataProvider.Settings.AggregatesPosition = TryCast(result, DataProviderSettings).AggregatesPosition
            dataProvider.Settings.AggregatesLevel = TryCast(result, DataProviderSettings).AggregatesLevel
        End If
    End Sub
End Class


public class XmlaProviderSerializer : DataProviderSerializer
{
    public override IEnumerable<Type> KnownTypes
    {
        get
        {
            return XmlaPivotSerializationHelper.KnownTypes;
        }
    }
}

Public Class XmlaProviderSerializer
    Inherits DataProviderSerializer
    Public Overrides ReadOnly Property KnownTypes() As IEnumerable(Of Type)
        Get
            Return XmlaPivotSerializationHelper.KnownTypes
        End Get
    End Property
End Class

So the last step is to serialize the provider and deserialize it.

Using the XmlaProviderSerializer

string lastXmlaSerializadProvider = "";

private void serializeXmlaBtn_Click(object sender, EventArgs e)
{
    XmlaProviderSerializer serializeProvider = new XmlaProviderSerializer();
    this.lastXmlaSerializadProvider = serializeProvider.Serialize(this.radPivotGrid1.DataProvider);
}

private void deserializeXmlaBtn_Click(object sender, EventArgs e)
{
    XmlaProviderSerializer provider = new XmlaProviderSerializer();
    provider.Deserialize(this.radPivotGrid1.DataProvider, this.lastXmlaSerializadProvider);
}

Private lastXmlaSerializadProvider As String = ""
Private Sub serializeXmlaBtn_Click(sender As Object, e As EventArgs) Handles serializeXmlaBtn.Click
    Dim serializeProvider As New XmlaProviderSerializer()
    Me.lastXmlaSerializadProvider = serializeProvider.Serialize(Me.RadPivotGrid1.DataProvider)
End Sub
Private Sub deserializeXmlaBtn_Click(sender As Object, e As EventArgs) Handles deserializeXmlaBtn.Click
    Dim provider As New XmlaProviderSerializer()
    provider.Deserialize(Me.RadPivotGrid1.DataProvider, Me.lastXmlaSerializadProvider)
End Sub

See Also

Was this article helpful? Yes No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy