Examples of Serialize/Deserialize without using an XML File

All the examples of serializing / deserializing show this being written to a XML file.   But can't find a good example where it is serialized to a string and then retrieved.   

The intention is that the serialization is stored as a string that can be stored in a database.   I have some method which convert from stream to String and vice versa but these seem to cause problems on deserialization.   So I'm looking for a simple example which will serialize to a string and then deserialize that string back.

5 Replies

VS Vijayarasan Sivanandham Syncfusion Team January 15, 2021 05:19 PM UTC

Hi Spotty, 
Thank you for contacting Syncfusion support.

Based on provided information we have analyzed and checked your requirement of Serialization to a string and then Deserialization that string back in SfDataGrid. We also regret to inform you that​​​ currently, Our SfDatagrid source architecture does not provide support to achieve your requirement of “Examples of Serialize/Deserialize without using an XML File in SfDataGrid”.

Regards,
Vijayarasan S 



SP Spotty January 15, 2021 05:49 PM UTC

Well that seems rather limited - requiring that you can only serialize a Datagrid to an XML file.   

What would happen if you wanted to use JSON or serialize to some other store other than a file.

Perhaps that can be logged as a feature request.


MA Mohanram Anbukkarasu Syncfusion Team January 18, 2021 01:19 PM UTC

Hi Spotty, 

Sorry for the inconvenience.  

It is not possible to perform serialization and deserialization without using xml file. However you can make use of the below given workaround to store and restore the details in the xml file to a string and vise versa. 

Code example :  

string xmlString; 
 
private void btn_serialize_Click(object sender, EventArgs e) 
{ 
    using (FileStream fileStream = File.Create("grid.xml")) 
    { 
        this.sfDataGrid1.Serialize(fileStream); 
    } 
 
    if (File.Exists("grid.xml")) 
        xmlString = System.IO.File.ReadAllText("grid.xml"); 
} 


private void btn_deserialize_Click(object sender, EventArgs e) 
{ 
    if (File.Exists("grid.xml")) 
    { 
        System.IO.File.WriteAllText(@"grid.xml", xmlString); 
 
        using (FileStream fileStream = File.OpenRead("grid.xml")) 
        { 
            this.sfDataGrid2.Deserialize(fileStream); 
        } 
    } 
} 

We have prepared a sample using the above given code example and it is available in the following link for your reference.  


Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 



SP Spotty January 18, 2021 05:00 PM UTC

Thanks for trying but that solution is really a horrible solution. as it depends upon the file being written and slows things down, is hardly scalable to multiple grids on a page using 1 filename or then requires multiple filenames.

Trying different things in the code - it is possible to achieve without an XML file using Memorystreams.    This string can then be stored in a variety of sources.   In my case a SQL database.    The code could easily be tested further and added as an overload allowing serialize/deserialize to a string.


'Serialize to a String
   Dim stream2 As New MemoryStream
        sfdDG.Serialize(stream2)
        Dim s = stream2.ToArray
        Dim s1 = Encoding.ASCII.GetString(stream2.ToArray())
        stream2.Close()

        serializeString = s1


Deserialize from a String'

  Dim byteArray() As Byte = Encoding.UTF8.GetBytes(SerializedString)
                        Dim Stream4 = New MemoryStream(byteArray)
                        sfdDG.Deserialize(Stream4)
                        Stream4.Close()





MA Mohanram Anbukkarasu Syncfusion Team January 19, 2021 01:07 PM UTC

Hi Spotty, 

Thanks for the update.  

We are glad to know that you have achieved your requirement at your end. Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 


Loader.
Up arrow icon