.net – FlowDocument contents as text

flowdocumentnetwpfxaml

What is the best way to get back the XAML/XML value of a FlowDocument?

I noticed there isn't a .Value, .Text, .Caption, .ToXml(), etc…

UDPATE:
I'd like to be able to get access to it initially to serialize to disk or database. Treat it as its own document format. Later translating it to other formats would be nice.
Also been wondering:
Any equivalent to a hyperlink (opens in new browser window) in a FlowDocument? Any workaround?

Best Answer

In response to your first question, you can use the XamlWriter to get XAML as a string. For example:

XamlWriter.Save(flowDocument);

David Veeneman has a nice example of this in the FlowDocumentToXamlConverter implementation he created for his Bindable WPF RichTextBox.

His converter will also convert back from a string containing XAML to a FlowDocument, which should handle your persistence requirement.

On your second question, you can embed WPF's Hyperlink element in a FlowDocument. These StackOverflow questions have more details:

  1. Handle links in a FlowDocument
  2. How can I get a FlowDocument Hyperlink to launch a browser

Tip: You should split your questions apart to get faster and better responses on StackOverflow. It prevents "oh, just one more thing..." fatigue!

Related Topic