Save and load diagram drawings with custom file extensions

Applies to: Nevron Diagram for .NET

How to save and load diagram drawings with custom file extensions?

The following code demonstrates how to save and load Nevron diagram drawing document to a file with arbitrary extension:

[C#]
private bool SaveToFile(NDrawingDocument document, string fileName)
{
      NPersistencyManager manager = new NPersistencyManager();
      manager.PersistentDocument.Sections.Add(new NPersistentSection("Drawing", document));
      return manager.SaveToFile(fileName, Nevron.Serialization.PersistencyFormat.XML, null);
}
 
private NDrawingDocument LoadFromFile(string fileName)
{
      NPersistencyManager manager = new NPersistencyManager();
      manager.LoadFromFile(fileName, Nevron.Serialization.PersistencyFormat.XML, null);
      return (NDrawingDocument)manager.PersistentDocument.Sections.GetByName("Drawing").Object;
}

[VB.NET]
Private Function SaveToFile(document As NDrawingDocument, fileName As String) As Boolean
    Dim manager As New NPersistencyManager()
    manager.PersistentDocument.Sections.Add(New NPersistentSection("Drawing", document))
    Return manager.SaveToFile(fileName, Nevron.Serialization.PersistencyFormat.XML, Nothing)
End Function
 
Private Function LoadFromFile(fileName As String) As NDrawingDocument
    Dim manager As New NPersistencyManager()
    manager.LoadFromFile(fileName, Nevron.Serialization.PersistencyFormat.XML, Nothing)
    Return DirectCast(manager.PersistentDocument.Sections.GetByName("Drawing").[Object], NDrawingDocument)
End Function

Article ID: 201, Created On: 8/17/2011, Modified: 8/17/2011

Comments (1)

Luc Ouimet

In the example above when you save over the same file without deleting it first a new section is added each time you save (didn't test if the section is also added to the loaded document).

I first check if the section exist then act accordingly:
...
NPersistentSection sectionPersist = manager.PersistentDocument.Sections.GetByName("Drawing");
if(sectionPersist==null)
manager.PersistentDocument.Sections.Add(new NPersistentSection("Drawing", document));
else{
sectionPersist.Object = document;
}
return manager.SaveToFile(fileName, Nevron.Serialization.PersistencyFormat.XML, null);

3/16/2013 at 7:26 AM