1

I spent my day searching on the internet a solution to my problem without success ... That seems simple enough in theory.

I have a RichTextBox in XAML. I have in my database a field named "content" type "LongText"

I just want my database saves the contents of the RichTextBox with simple style (bold, italic, underline) and that I can call my database, fill my RichTextBox content stored in my database while keeping the style of the text.

After that, I'll have to show these data as a blog but ... must already successfully save and load data.

XAML :

<RichTextBox x:Name="nameTextEditor" TabIndex="2" Grid.Row="2" SpellCheck.IsEnabled="True">

C#

FlowDocument doc = new FlowDocument();
        StringReader sr = new StringReader(monArticle.Content);
        XmlReader xmlReader = XmlReader.Create(sr);
        Section sec = XamlReader.Parse(monArticle.Content) as Section;
        while (sec.Blocks.Count > 0)
        {
            var block = sec.Blocks.FirstBlock;
            sec.Blocks.Remove(block);
            doc.Blocks.Add(block);
        }
        nameTextEditor.Document = doc;

Don't work with error : "Exception XamlParseException" "Données non valides au niveau racine. Ligne 1, position 1."

This is not the only code I tried but I test without understanding a word ... Why it is not so easy to "take the contents of the RichTextBox" and "send in my database"?!

Thanks for all help

1 Answer 1

6
    var doc = rtb.Document;
    var range = new TextRange(doc.ContentStart, doc.ContentEnd);
    var ms = new MemoryStream();
    range.Save(ms, DataFormats.Rtf);
    ms.Seek(0, SeekOrigin.Begin);

    var rtfString=new StreamReader(ms).ReadToEnd();

Now send rtfString to the database. To display it back in the richtext box:

var doc = rtb.Document;
var range = new TextRange(doc.ContentStart, doc.ContentEnd);
var ms = new MemoryStream();
var sw = new StreamWriter(ms);
sw.Write(rtfString);
sw.Flush();
ms.Seek(0, SeekOrigin.Begin);
range.Load(ms, DataFormats.Rtf);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I try it tomorrow on job-time
... (precision) with littles modifications

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.