Reputation: 1477
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
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