Saving FlowDocument to SQL Server

后端 未结 3 935
春和景丽
春和景丽 2020-12-19 19:09

I need to save WPF FlowDocuments to SQL Server. What is the best format for doing that? String? Blob? Does it matter in a document less than 5K words or so?

相关标签:
3条回答
  • 2020-12-19 19:16

    FlowDocument is not serializable so SWeko's answer above will not work. You can use the methods below to get the FlowDocument to and from a Xaml string which can then be saved in the database using nvarchar(max).

        var stringReader = new StringReader(info);
        var xmlTextReader = new XmlTextReader(stringReader);
        return (FlowDocument)XamlReader.Load(xmlTextReader);
    

    and

        var infoString = XamlWriter.Save(info);
    
    0 讨论(0)
  • 2020-12-19 19:25

    You can serialize FlowDocument using the TextRange class. You can even use the RTF format. Saving:

    FlowDocument docToSave; // Lets suppose this var is initialized.
    var tr = new TextRange(docToSave.ContentStart,docToSave.ContentEnd);
    var dst = new MemoryStream();
    tr.Save(dst, DataFormats.Rtf);
    dst.Close();
    

    And loading:

    FlowDocument docToLoad = new FlowDocument();
    var tr = new TextRange(docToLoad.ContentStart,docToLoad.ContentEnd);
    Stream src; // Lets suppose it is initialized.
    tr.Load(src, DataFormats.Rtf);
    src.Close();
    

    See also https://www.wpf-tutorial.com/rich-text-controls/how-to-creating-a-rich-text-editor/

    0 讨论(0)
  • 2020-12-19 19:33

    If you just want to store the FlowDocument objects in a database, without any processing, I would recommend using binary serialization, and storing the resulting byte array into a varbinary(max). This is fast and scales well.

    However, if you already have the FlowDocuments as XML files, than it would be easier just to dump them into a nvarchar(max) field, with no (added) serialization/deserialization overhead. This scales trivially for values under 8k, and then performs kinda OK until you hit around the 10MB mark.

    0 讨论(0)
提交回复
热议问题