1

I have some text that is being read in from the database. This text is a string representation of a xaml section object. This text is going to differ based on selections from a combo box. Here is an example of some text:

<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve" TextAlignment="Left" LineHeight="Auto" IsHyphenationEnabled="False" xml:lang="en-us" FlowDirection="LeftToRight" NumberSubstitution.CultureSource="User" NumberSubstitution.Substitution="AsCulture" FontFamily="Verdana" FontStyle="Normal" FontWeight="Normal" FontStretch="Normal" FontSize="11" Foreground="#FF000000" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.AnnotationAlternates="0" Typography.ContextualAlternates="True" Typography.HistoricalForms="False" Typography.Kerning="True" Typography.CapitalSpacing="False" Typography.CaseSensitiveForms="False" Typography.StylisticSet1="False" Typography.StylisticSet2="False" Typography.StylisticSet3="False" Typography.StylisticSet4="False" Typography.StylisticSet5="False" Typography.StylisticSet6="False" Typography.StylisticSet7="False" Typography.StylisticSet8="False" Typography.StylisticSet9="False" Typography.StylisticSet10="False" Typography.StylisticSet11="False" Typography.StylisticSet12="False" Typography.StylisticSet13="False" Typography.StylisticSet14="False" Typography.StylisticSet15="False" Typography.StylisticSet16="False" Typography.StylisticSet17="False" Typography.StylisticSet18="False" Typography.StylisticSet19="False" Typography.StylisticSet20="False" Typography.Fraction="Normal" Typography.SlashedZero="False" Typography.MathematicalGreek="False" Typography.EastAsianExpertForms="False" Typography.Variants="Normal" Typography.Capitals="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.EastAsianWidths="Normal" Typography.EastAsianLanguage="Normal" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" Typography.StylisticAlternates="0"><Paragraph><Run>Hello World      </Run></Paragraph></Section>

What I need to take this text and display it, properly formatted, in a control on the application. I believe that, in order to do this, I would need a FlowDocumentViewer (I am using FlowDocumentScrollViewer).

What I cannot figure out is how to get the text into a flow document so that I can associate it (bind it) with the Document viewer (and I am a noob at this).

Can anyone help?

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
The Sheek Geek
  • 4,126
  • 6
  • 38
  • 48

2 Answers2

6

You can use XamlReader.Parse to create a Section from the string which you then can add to the document.

H.B.
  • 166,899
  • 29
  • 327
  • 400
1

Let me add the complete code:

public static FlowDocument ToFlowDocument(string xmlString)
{
    var result = new FlowDocument();

    if (!string.IsNullOrEmpty(xmlString))
    {
        result.Blocks.Add((Section)XamlReader.Parse(xmlString));
    }

    return result;        
}
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116