Articles in this section
Category / Section

Adding OldToNewDeserializationBinder for loading old edp file in new version.

6 mins read

Convert old versioned EDP/EDD file in new version

To overcome the problems while loading old versioned EDP/EDD files in newer version projects, some conversion is needed in the WinForms Diagram. For example, if your EDP/EDD file version is 3.0 and the project version is 7.0, initially place the old versioned EDP/EDD files in the bin location of `<OldToIntermediate sample>`. After running the sample, it will generate an EDP/EDD file of the intermediate version.

Then, the generated intermediate file must be placed in the bin location of `<IntermediateToNew Sample>`. After running the sample, it will generate a new EDP/EDD file. This file can be loaded into the newer version projects, and the problem won’t occur.

Location for the Converter Sample:

C:\Users\<UserName>\AppData\Local\Syncfusion\EssentialStudio\<Installed Version>\Windows\Diagram.Windows\Samples\Converter

 

 

While using the EDD/EDP files of older version and loading in newer version it will throw Serialization exception. To avoid that exception the assembly resolver code must be used.

Add the below code snippet in your customized sample:

[C#]

 
public void LoadPalette1(string filename)
        {
            SymbolPalette symbolPalette = null;
            FileStream iStream;
            IFormatter formatter = new BinaryFormatter();
            ((BinaryFormatter)formatter).AssemblyFormat = FormatterAssemblyStyle.Simple;
            ((BinaryFormatter)formatter).TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
            ((BinaryFormatter)formatter).FilterLevel = TypeFilterLevel.Low;
            //Assembly resolver
            formatter.Binder = new OldToNewDeserializationBinder();
            iStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            symbolPalette = (SymbolPalette)formatter.Deserialize(iStream);
        }
 

 

[C#]

//Use the below sealed class for Assembly resolver
public sealed class OldToNewDeserializationBinder : SerializationBinder
    {
public override Type BindToType(string assemblyName, string typeName)
        {
            Type typeToDeserialize;
 
            // For each assemblyName/typeName that you want to deserialize to
            // a different type, set typeToDeserialize to the desired type.
            string assem = Assembly.GetExecutingAssembly().FullName;
 
            if (assemblyName.IndexOf("Syncfusion.Diagram") != -1 && assemblyName.IndexOf("Version", 0) != -1)
            {
                // find "Version" substring
                int nIdxStart = assemblyName.IndexOf("Version", 0);
                int nIdxEnd = assemblyName.IndexOf(",", nIdxStart);
 
                int nIdxRplStart = assem.IndexOf("Version", 0);
                int nIdxRplEnd = assem.IndexOf(",", nIdxRplStart);
 
                // replace whole "Version" substring
                assemblyName = assemblyName.Replace(
                    assemblyName.Substring(nIdxStart, nIdxRplStart + (nIdxEnd - nIdxStart)),
                    assem.Substring(nIdxRplStart, nIdxRplStart + (nIdxRplEnd - nIdxRplStart)));
            }
 
            if (typeName.IndexOf("LineNode") != -1)
            {
                // types binding to ensure loading previous versions
                typeName = typeName.Replace("LineNode", "Line");
            }
 
            // The following line of code returns the type.
            typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
 
            return typeToDeserialize;
        }
    }
  }
 

 

[VB]

 
Public Sub LoadPalette1(ByVal filename As String)
   Dim symbolPalette As SymbolPalette = Nothing
   Dim iStream As FileStream
   Dim formatter As IFormatter = New BinaryFormatter()
   CType(formatter, BinaryFormatter).AssemblyFormat = FormatterAssemblyStyle.Simple
   CType(formatter, BinaryFormatter).TypeFormat = FormatterTypeStyle.TypesWhenNeeded
   CType(formatter, BinaryFormatter).FilterLevel = TypeFilterLevel.Low
   'Assembly resolver
   formatter.Binder = New OldToNewDeserializationBinder()
   iStream = New FileStream(filename, FileMode.Open, FileAccess.Read)
   symbolPalette = CType(formatter.Deserialize(iStream), SymbolPalette)
End Sub
 

 

[VB]

Public NotInheritable Class OldToNewDeserializationBinder
 Inherits SerializationBinder
Public Overrides Function BindToType(ByVal assemblyName As String, ByVal typeName As String) As Type
   Dim typeToDeserialize As Type
 
   ' For each assemblyName/typeName that you want to deserialize to
   ' a different type, set typeToDeserialize to the desired type.
   Dim assem As String = System.Reflection.Assembly.GetExecutingAssembly().FullName
 
   If assemblyName.IndexOf("Syncfusion.Diagram") <> -1 AndAlso assemblyName.IndexOf("Version", 0) <> -1 Then
    ' find "Version" substring
    Dim nIdxStart As Integer = assemblyName.IndexOf("Version", 0)
    Dim nIdxEnd As Integer = assemblyName.IndexOf(",", nIdxStart)
 
    Dim nIdxRplStart As Integer = assem.IndexOf("Version", 0)
    Dim nIdxRplEnd As Integer = assem.IndexOf(",", nIdxRplStart)
 
    ' replace whole "Version" substring
    assemblyName = assemblyName.Replace(assemblyName.Substring(nIdxStart, nIdxRplStart + (nIdxEnd - nIdxStart)), assem.Substring(nIdxRplStart, nIdxRplStart + (nIdxRplEnd - nIdxRplStart)))
   End If
 
   If typeName.IndexOf("LineNode") <> -1 Then
    ' types binding to ensure loading previous versions
    typeName = typeName.Replace("LineNode", "Line")
   End If
 
   ' The following line of code returns the type.
   typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName))
 
   Return typeToDeserialize
End Function
End Class
 

Conclusion

I hope you enjoyed enjoyed learning about adding `OldToNewDeserializationBinder` for loading old versioned EDP files in a new version.

You can refer to our WinForms Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusio, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied