5

I have a very simple RichTextBox control in a silverlight 5 application.

<RichTextBox x:Name="rtbControl" 
    Height="Auto" 
    ContentChanged="rtbControl_ContentChanged" />

As the data is changed, i capture it in a local variable as so:

    private void rtbControl_ContentChanged(object sender, ContentChangedEventArgs e)
    {
        RichTextContent = rtbControl.Xaml;
    }

All works. However, if I type in just a few words and examine the <Paragraph> markup, it is HUGE! A snippet is below. Is there a way to not have all the Typography stuff in the markup? Why is it there and is it needed? Is there a way to remove it?

<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph FontSize="11" FontFamily="Segoe UI" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" 
CharacterSpacing="0" Typography.AnnotationAlternates="0" Typography.EastAsianExpertForms="False" Typography.EastAsianLanguage="Normal" 
Typography.EastAsianWidths="Normal" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" 
Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" 
Typography.ContextualAlternates="True" Typography.StylisticAlternates="0" 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.Capitals="Normal" Typography.CapitalSpacing="False" Typography.Kerning="True" Typography.CaseSensitiveForms="False" 
Typography.HistoricalForms="False" Typography.Fraction="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.SlashedZero="False"
 Typography.MathematicalGreek="False" Typography.Variants="Normal" TextOptions.TextHintingMode="Fixed" TextOptions.TextFormattingMode="Ideal" 
TextOptions.TextRenderingMode="Auto" TextAlignment="Left" LineHeight="0" LineStackingStrategy="MaxHeight"><Run>was it a </Run><Run TextDecorations="Underline">bad 
</Run><Run>blah? or </Run></Paragraph></section>
tsiorn
  • 2,236
  • 1
  • 22
  • 26
  • Ouah, that's big :( I think it's needed for persistence to ensure you get the exact formatting. – jv42 May 04 '12 at 07:47
  • What are you trying to do? Maybe you can use the Text, or selected text properties? See this http://msdn.microsoft.com/fr-fr/library/ff426926%28v=vs.95%29.aspx official sample. – jv42 May 04 '12 at 07:47
  • Im giving users ability to create pieces of a document and I need to give them rich text formatting capabilities. Users input gets pushed to a database. I do need to capture whatever formatting they do. The way it is now, the user can type in 4 words on 3 lines and its already over 4000 characters of text. – tsiorn May 04 '12 at 12:30
  • 1
    Well, I see two ways to reduce the size of the stuff: compression and parsing the XML to remove all default attributes from Paragraph & co. – jv42 May 04 '12 at 13:00
  • But I don't see a built in solution, sorry. – jv42 May 04 '12 at 13:00
  • Thanks for looking... Im going with a solution of parsing out some of the extra attributes. – tsiorn May 04 '12 at 16:30
  • I agree with your choice, it's probably the best for your case. – jv42 May 05 '12 at 09:27

1 Answers1

4

I thought I would share my solution to help others... its been in place for several months and I havent seen any bad side effects.

I created some extention methods to assist in removing the Typography attributes.

public static class RichTextBoxExtensions {
        // <summary>
        /// Removes any xml tag with the prefix given from a string
        /// </summary>
        /// <param name="XMLString"></param>
        /// <param name="TagPrefix"></param>
        /// <returns></returns>
        public static string RemoveXMLAttributesFromNode(this string XMLString, string prefix)
        {
            //Match [Prefix][any number of Not =][=]["][any number of Not "]["][ ] <-must have space!!!
            string replace = prefix + "[^\\=]*=\"[^\"]*\" ";
            return Regex.Replace(XMLString, replace, string.Empty);
        }
        /// <summary>
        /// Removes any xml tag with prefixed by an element in unWanted
        /// </summary>
        /// <param name="XMLString"></param>
        /// <param name="TagPrefix"></param>
        /// <returns></returns>
        public static string RemoveXMLAttributesFromNode(this string XMLString, List<string> unWanted)
        {

            foreach (string prefix in unWanted)
            {
                //Match [Prefix][any number of Not =][=]["][any number of Not "]["][ ] <-must have space!!!
                string replace = prefix + "[^\\=]*=\"[^\"]*\" ";
                XMLString = Regex.Replace(XMLString, replace, string.Empty);
            }
            return XMLString;
        }

Next, right before I save my string to the database, I remove the bad attributes as so:

        List<string> badAttributes = new List<string>();
        badAttributes.Add("Typography");
        var ffText = ffi.FreeFormText == null ? null : ffi.FreeFormText.RemoveXMLAttributesFromNode(badAttributes);

I have not seen any issues when re-rendering the RTF after removing these attributes. Hope this solution helps someone!

tsiorn
  • 2,236
  • 1
  • 22
  • 26