将 Richtextbox 数据连同格式一起保存在数据库中

发布于 2024-09-07 01:13:37 字数 145 浏览 3 评论 0原文

我的一个应用程序 (WPF) 中有一个富文本框。现在我想将富文本框的数据及其格式(例如粗体、彩色等)存储到数据库(SQL Server)中。目前,我将文本框的整个 XAML 存储在数据库字段中。但是,我不确定这是否是正确的方法。期待您的建议!

I have a rich text box in one of my applications (WPF). Now I want to store the data of the rich text box along with its formatting (e.g. bold, colored etc.) into a database (SQL Server). Currently I am storing the whole XAML of the text box in a database field. however, I am not sure whether this is the right approach. Looking forward to your suggestions!

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

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

发布评论

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

评论(1

野生奥特曼 2024-09-14 01:13:37

另一种方法是以 RTF 格式存储数据,这种格式可能比 Xaml 稍微紧凑一些,并且具有可以轻松导入到无法解析 Xaml 的其他应用程序的额外好处:

string GetContentAsRTF(RichTextBox rtb)
{
    var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);

    using (var stream = new MemoryStream())
    using (var reader = new StreamReader(stream))
    {
        range.Save(stream, DataFormats.Rtf);
        stream.Position = 0;
        return reader.ReadToEnd();
    }
}

An alternative is to store the data in RTF format, which may be slightly more compact than Xaml and offers the additional benefit of being easily imported into other applications that can't parse Xaml:

string GetContentAsRTF(RichTextBox rtb)
{
    var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);

    using (var stream = new MemoryStream())
    using (var reader = new StreamReader(stream))
    {
        range.Save(stream, DataFormats.Rtf);
        stream.Position = 0;
        return reader.ReadToEnd();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文