我需要将WPF FlowDocuments保存到SQL Server中。最好的格式是什么?字符串?二进制大对象(Blob)?在不到5K字的文档中是否重要?
我需要将WPF FlowDocuments保存到SQL Server中。最好的格式是什么?字符串?二进制大对象(Blob)?在不到5K字的文档中是否重要?
FlowDocument不可序列化,因此上面的SWeko的答案将无法使用。 您可以使用以下方法将FlowDocument转换为Xaml字符串,然后使用nvarchar(max)保存在数据库中。
var stringReader = new StringReader(info);
var xmlTextReader = new XmlTextReader(stringReader);
return (FlowDocument)XamlReader.Load(xmlTextReader);
and
var infoString = XamlWriter.Save(info);
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/。