Farax
Farax

Reputation: 1477

Saving richtextbox data inside a database along with the formatting

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!

Upvotes: 2

Views: 3086

Answers (1)

Tim Coulter
Tim Coulter

Reputation: 8927

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

Upvotes: 2

Related Questions