Search code examples
c#winformsdevexpressdevexpress-windows-ui

Saving and opening multiple DiagramControls in single file


In my application, I've shown two diagram controls i.e. one for front card and one for back card. Now after adding any number of shapes, lines and images. I added one dashboard designer form with data source as well. I want to save both this diagram controls, dashboard designer with data source properties with extra custom properties (like my custom class with primitive data types and custom data types) in single file as a template and after that when I open that single template file, both the diagram controls, dashboard designer and other properties will be loaded as previously with all the shapes, lines and images.


Solution

  • For saving multiple diagram controls and connection properties, I have created my own class having members to hold these information like

    public class TemplateClass
    {
        public string[] DiagramControlsXMLs;
        public string SqlDataSourceXML { get; set; }
        public string TemplateName { get; set; }    
        ///add more other properties
    }
    

    Then at the time of saving the template, save the diagram controls temporarily and store its contents in TamplateClass string variables like

    //save it with temporary name
    diagControlA.SaveDocument(strTempFileName);
    string strXML = "";
    if (File.Exists(strTempFileName))
    {
        //read XML based diagram control file
        strXML = File.ReadAllText(strTempFileName);
        File.Delete(strTempFileName);
    }
    
    if (objTemplate.DiagramControlsXMLs == null)
        objTemplate.DiagramControlsXMLs = new string[0];
    
    Array.Resize(ref objTemplate.DiagramControlsXMLs, objTemplate.DiagramControlsXMLs.Length + 1);
    objTemplate.DiagramControlsXMLs[objTemplate.DiagramControlsXMLs.Length - 1] = strXML;
    

    Also to save the connection properties, save its corresponding XML temporarily and load in template class variable like

    XElement objXE = GM.objDataSources.objSqlDataSource.SaveToXml();
    objXE.Save(strTempFileName);
    
    if (File.Exists(strTempFileName))
    {
        //store Sql Data source xml string
        objTemplate.SqlDataSourceXML = File.ReadAllText(strTempFileName);
        File.Delete(strTempFileName);
    }
    

    And finally serialize the template class object into json based string and save it as a file by name and extension like

    DataContractJsonSerializer objJS = new DataContractJsonSerializer(typeof(TemplateClass));
    MemoryStream objMS = new MemoryStream();
    objJS.WriteObject(objMS, objTemplate);
    
    string str = Encoding.Default.GetString(objMS.ToArray());
    using (StreamWriter objSW = new StreamWriter(strTemplateFilePath, false))
    {
        objSW.Write(str);
        objSW.Close();
    }
    

    Now to open this template file, apply the reverse procedure the load the diagram controls with their items and connections parameters with their properties.