views:

608

answers:

1

I have some HTML, which i am converting to XAML using the library provided by Microsoft

string t = HtmlToXamlConverter.ConvertHtmlToXaml(mail.HtmlDataString,true);

now, from http://stackoverflow.com/questions/1449121/how-to-insert-xaml-into-richtextbox i am using the following:

private static FlowDocument SetRTF(string xamlString)
{
    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)
        doc.Blocks.Add(sec.Blocks.FirstBlock);
    return doc;
}

This however keeps crashing unfortunately =/ Does anyone have any clue on how to display XAML text in a RichTextBox please?

EDIT: Example of html i'm trying to convert

<FlowDocument xml:space="preserve" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&gt;&lt;Table&gt;&lt;TableRowGroup&gt;&lt;TableRow&gt;&lt;TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Paragraph>Top stories for<LineBreak />Tuesday,<LineBreak />May 4, 2010</Paragraph></TableCell><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Paragraph><Hyperlink Foreground="#555555" NavigateUri="http://adverts.timesofmalta.com/openx/www/delivery/ck.php?zoneid=7" /></Paragraph></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black" RowSpan="10"><Paragraph FontSize="17px">Latest News</Paragraph></TableCell><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Paragraph><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/local"&gt;News&lt;/Hyperlink&gt;&lt;/Span&gt;&lt;LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/code-breaker"&gt;Code breaker</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/persuni-li-spiccaw-il-habs-minhabba-kazijiet-ta-vat"&gt;106 in prison since 2004 for VAT violations</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/mumn-disappointed-over-deployment-of-nurses"&gt;MUMN disappointed over deployment of nurses</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/press-digest"&gt;Press digest</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/world-news"&gt;World News</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/new-iceland-volcano-ash-grounds-flights-in-ireland"&gt;Ash cloud returns: Ryanair cancels Malta flight from Dublin</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/man-arrested-over-ny-bomb-attempt-reports"&gt;Man arrested over NY bomb attempt: reports</Hyperlink><LineBreak /><LineBreak /></Paragraph></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black" RowSpan="10"><Paragraph FontSize="17px">The Times<LineBreak /></Paragraph></TableCell><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Paragraph><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/local"&gt;News&lt;/Hyperlink&gt;&lt;/Span&gt;&lt;LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/injured-president-returns-from-china"&gt;Injured President returns from China</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/law-breakers-to-be-barred-from-public-contracts"&gt;VAT fraud - Law breakers to be barred from public contracts</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/warden-assaulted-on-parking-ticket"&gt;Warden 'assaulted' on parking ticket</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/against-the-law"&gt;Against the law?</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/linkin-park-the-kooks-for-isle-of-mtv-concert"&gt;Linkin Park, The Kooks for Isle of MTV concert</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/world-news"&gt;World News</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/mumbai-attacks-gunman-convicted"&gt;Mumbai attacks gunman convicted</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/ireland-britain-eye-new-ash-flight-bans"&gt;Ireland, Britain eye new ash flight bans</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/white-male-suspect-linked-to-times-square-car-bomb"&gt;White male suspect linked to Times Square car bomb</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/iranian-president-blasts-us-for-nuclear-threats"&gt;Iranian President blasts US for nuclear threats</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/strikes-to-disrupt-greek-flights-as-from-today"&gt;Strikes to disrupt Greek flights as from today</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/business"&gt;Business &amp; Finance</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/business/ecb-returns-to-variable-rate-tender-procedures"&gt;Money market report - week ended April 30 - ECB returns to variable rate tender procedures</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/business/new-chairman-for-volksbank-malta-ltd"&gt;New chairman for Volksbank Malta Ltd</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/business/daily-currency-report"&gt;Daily currency report</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/business/financial-news"&gt;Financial news</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/business/european-stocks-shake-off-early-jitters-and-gain-ground"&gt;Market overview - European stocks shake off early jitters and gain ground</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/sport"&gt;Sport&lt;/Hyperlink&gt;&lt;/Span&gt;&lt;LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/sport/valletta-part-with-obrien"&gt;Football - Valletta part with O'Brien</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/sport/title-clinching-goal-just-reward-for-ebullient-bajada"&gt;Football - BOV Premier League - Title-clinching goal just reward for ebullient Bajada</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/sport/draw-enough-for-sliema-to-secure-third-place"&gt;Football - BOV Premier League - Draw enough for Sliema to secure third place</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/sport/bov-premier-league-highlights"&gt;Football - BOV Premier League highlights</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/sport/mqabba-finish-on-a-high"&gt;Football - Mqabba finish on a high</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/opinion"&gt;Opinion&lt;/Hyperlink&gt;&lt;/Span&gt;&lt;LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/opinion/editorial"&gt;Editorial - Keeping to the right financial track</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/opinion/habitats-of-the-maltese-islands-7"&gt;One World - Habitats of the Maltese islands (7)</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/opinion/guaranteeing-our-heritages-future"&gt;Guaranteeing our heritage's future</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/opinion/mixed-data-on-environment"&gt;Mixed data on environment</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/opinion/an-issue-of-accountability"&gt;An issue of accountability</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/letters"&gt;Letters&lt;/Hyperlink&gt;&lt;/Span&gt;&lt;LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/letters/service-deserving-of-a-national-award"&gt;Service deserving of a national award</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/letters/vulgar-perception-of-latin-language"&gt;Vulgar perception of Latin language</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/letters/chemical-spray-used-on-potato-crops"&gt;Chemical spray used on potato crops</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/letters/blasted-from-the-past-1"&gt;Blasted from the past (1)</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/letters/blasted-from-the-past-2"&gt;Blasted from the past (2)</Hyperlink><LineBreak /><LineBreak /></Paragraph></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow></TableRowGroup></Table></FlowDocument>
+1  A: 

I assume it crashes on Add() because you are adding a FrameworkContentElement which already has a logical parent. You must first remove a Block from its parent and only then add it to a different collection. Try this:

private static FlowDocument SetRTF(string xamlString)
{
    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);
    }
    return doc;
}
wpfwannabe
unfortunately this is also crashing as Section sec is remaining null =/ I've added a sample of what i'm trying to convert
Erika
It appears your XAML string actually represents a `FlowDocument`. Then things are much simpler. Just do a `return XamlReader.Load(xmlReader) as FlowDocument;` and you're done.
wpfwannabe
This worked like a charm! THANKS SO MUCH! really appreciated!!
Erika