将 FlowDocument 保存到 SQL Server

发布于 2024-08-25 13:34:57 字数 83 浏览 8 评论 0原文

我需要将 WPF FlowDocuments 保存到 SQL Server。这样做的最佳格式是什么?细绳?斑点?这对于少于 5K 字左右的文档有影响吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南巷近海 2024-09-01 13:34:57

FlowDocument 不可序列化,因此 SWeko 的上述答案将不起作用。
您可以使用以下方法从 Xaml 字符串获取 FlowDocument,然后可以使用 nvarchar(max) 将其保存在数据库中。

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

    var infoString = XamlWriter.Save(info);

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);
月棠 2024-09-01 13:34:57

如果您只想将 FlowDocument 对象存储在数据库中,而不进行任何处理,我建议使用二进制序列化,并将生成的字节数组存储到 varbinary(max) 中。这是快速且可扩展的。

但是,如果您已经将 FlowDocuments 作为 XML 文件,那么将它们转储到 nvarchar(max) 字段中会更容易,而无需(添加)序列化/反序列化开销。对于 8k 以下的值,这可以轻松扩展,然后执行得还不错,直到达到 10MB 左右。

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.

伊面 2024-09-01 13:34:57

您可以使用 TextRange 类序列化 FlowDocument。您甚至可以使用 RTF 格式。保存:

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();

和加载:

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();

另请参阅 https://www.wpf-tutorial.com/rich-text-controls/how-to-creating-a-rich-text-editor/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文