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!
A:
Check this :
An extended RichTextBox to save and load HTML : http://www.codeproject.com/KB/edit/htmlrichtextbox.aspx
Samiksha
2010-06-21 06:29:34
Isn't there a way to do this with the existing rich text box in wpf?I dont want to replace this with another as I have already done the work the other way but i do not like it this way. So was hoping for another better way. Cheers
Farax
2010-06-22 10:52:19
+1
A:
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();
}
}
Tim Coulter
2010-07-10 20:07:15