0

I have Silverlight application that saves the RichTextBox in XAML, like this:

  <Comentario>
  <Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://www.schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Paragraph FontSize="22" FontFamily="Arial" 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 FontSize="22" FontFamily="Janda Apple Cobbler" Foreground="#FF000000">My TEXT</Run>
  </Paragraph>
  </Section>
  </Comentario>

I also have a local WPF application that must read the XAML. The richtextbox in WPF dont support XAML, so i have to convert this XAML to a FlowDocument. I have tried many ways of doing it, but i also get an error:

Code 1:

        StringReader stringReader = new StringReader(xamlString);
        System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
        Section sec = XamlReader.Load(xmlReader) as Section;
        FlowDocument doc = new FlowDocument();
        while (sec.Blocks.Count > 0)
        {
            var block = sec.Blocks.FirstBlock;
            sec.Blocks.Remove(block);
            doc.Blocks.Add(block);
        }

Error:

Primera excepción del tipo 'System.Windows.Markup.XamlParseException' en PresentationFramework.dll

Información adicional: 'Cannot create unknown type '{http://www.schemas.microsoft.com/winfx/2006/xaml/presentation}Section'.' Line number '1' and line position '2'.

Code 2: Using ParserContext

        System.Windows.Markup.ParserContext parserContext = new System.Windows.Markup.ParserContext();
        parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
        parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
        StringReader stringReader = new StringReader(xamlString);
        System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
        Section sec = XamlReader.Load(xmlReader,parserContext) as Section;
        FlowDocument doc = new FlowDocument();
        while (sec.Blocks.Count > 0)
        {
            var block = sec.Blocks.FirstBlock;
            sec.Blocks.Remove(block);
            doc.Blocks.Add(block);
        }

Error: Error 14 The best overloaded method match for 'System.Windows.Markup.XamlReader.Load(System.IO.Stream, System.Windows.Markup.ParserContext)' has some invalid arguments

Please help me, I need to find a way to read the XAML string created in Sirvelight in my local WPF application.

1 Answers1

0

First

http://www.schemas.microsoft.com/winfx/2006/xaml/presentation

should be

http://schemas.microsoft.com/winfx/2006/xaml/presentation

Assuming that is a typo, your next problem is using the XAML Parser. Even if Comentario, Section and Paragraph exist in WPF, they could live in a different namespace and likely have different properties.

Given that you are resigned to converting the XAML to a FlowDocument, perhaps it would be best to skip the XAML parser.

Why not just use XDocument instead instead of XamlReader?

Silver Solver
  • 2,310
  • 1
  • 13
  • 19
  • Hi Trigger. Thanks for your answer. I double check my silverlight code, and its saves sometimes the XAML with http://www.schemas.microsoft.com/winfx/2006/xaml/presentation and in other cases http://schemas.microsoft.com/winfx/2006/xaml/presentation. I remove the www. from the XAML String and "works", right now i move to another error, CharacterSpacing is not a property in WPF but is a property in Silverlight (it is not part of this case). Last question: Do you know how to prevent Silverlight save sometimes the XAML with www. and other cases without it? – Victor Hugo Paz Feb 19 '13 at 23:28
  • I expect that you will often encounter properties that either don't exist, or have different names in your conversion. As I said before, if you treat the XAML string as XML, you will not need to worry about namespace names or property names and you can simply focus on mapping one Silverlight type (defined in XML) to one WPF type. – Silver Solver Feb 20 '13 at 07:20