C# 使用rtf加载数据仅对以下数据进行例外

C# 使用rtf加载数据仅对以下数据进行例外,c#,html,wpf,C#,Html,Wpf,使用此代码在qpf中加载数据时 public static void LoadRTF(string rtf, RichTextBox richTextBox) { if (string.IsNullOrEmpty(rtf)) { throw new ArgumentNullException(); } TextRange textRange = new TextRange(richTextBox.

使用此代码在qpf中加载数据时

public static void LoadRTF(string rtf, RichTextBox richTextBox)
    {

        if (string.IsNullOrEmpty(rtf))
        {
            throw new ArgumentNullException();
        }

        TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

        //Create a MemoryStream of the Rtf content

        using (MemoryStream rtfMemoryStream = new MemoryStream())
        {
            using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream))
            {
                rtfStreamWriter.Write(rtf);
                rtfStreamWriter.Flush();
                rtfMemoryStream.Seek(0, SeekOrigin.Begin);

                //Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
                try
                {
                    textRange.Load(rtfMemoryStream, DataFormats.Rtf);
                }
                catch { Exception ex; }
            }
        }
    }
应用程序引发一个名为

“富文本格式”数据格式中的结构无法识别。\r\n参数名称:流

数据是

<span dir="LTR" style="font-size: 9pt; background: none repeat scroll 0% 0% transparent; font-family: &quot;verdana&quot;,&quot;sans-serif&quot;; color: #444444;">Abu Dhabi Retirement Pensions &amp; Benefits Fund has announced that the Fund has fully enabled 37 electronic services provided via its online portal that was launched on June 2012. This is an indication of the Fund&rsquo;s concern to convoy Abu Dhabi&rsquo;s Government Vision and the Fund&rsquo;s strategy. There are 17 services which are allocated for entities. 775 governmental, semi-governmental and private entities are using these services and 2716 transactions have been accomplished last July. 13 other electronic services were categorized as general services targeted the active members, pensioners, and entities in which these services were used 2056 times since they were launched.&nbsp;</span><span dir="LTR" style="font-size: 9pt; font-family: &quot;verdana&quot;,&quot;sans-serif&quot;; color: #444444;"><br /> <span style="background: none repeat scroll 0% 0% transparent;">Moreover, there are seven electronic services presented to the partners from governmental and semi-governmental entities in order to ease the exchanging of information and transactions among the entities and the Fund and to support Abu Dhabi&rsquo;s government strategy to develop services.&nbsp;</span><br /> <span style="background: none repeat scroll 0% 0% transparent;">12 electronic services are expected to be launched for pensioners and beneficiaries and active members in 2014 which will enhance the level of the provided services.</span></span> 
阿布扎比退休养老金&;福利基金宣布,该基金已通过2012年6月推出的在线门户网站全面启用37项电子服务。这是基金的一种迹象;s对阿布扎比护航的关注;政府愿景和基金;中国的战略。有17项服务分配给实体。775个政府、半政府和私营实体正在使用这些服务,去年7月完成了2716笔交易。13其他电子服务被归类为一般服务,针对在职会员、养老金领取者和自推出以来使用这些服务2056次的实体
此外,政府和半政府实体向合作伙伴提供了七项电子服务,以方便实体和基金之间的信息和交易交流,并支持阿布扎比;中国政府发展服务业的战略
预计2014年将为养老金领取者、受益人和在职会员推出12项电子服务,这将提高所提供服务的水平。
这是rtf格式,错误是什么{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}{\f3\fcharset0 verdana;}{\colortbl\red0\green0\BLUEN0;\red255\green255\BLUEN255\BLUEN255;\red68\BLUEN68;}\loch\hich\dbch\pard\ltrpar\ITA033\CFF2{\loch\f3\cf2\ltrch阿布扎比退休养老金和福利基金宣布,该基金已通过2012年6月推出的在线门户网站全面启用了37项电子服务。这表明该基金关注阿布扎比政府愿景和基金战略。共有17项服务分配给实体。775个政府、半政府和私营实体正在使用这些服务,去年7月已完成2716笔交易。13个其他电子服务被归类为一般服务,面向在职会员、养老金领取者,以及这些服务自启用以来使用了2056次的实体已启动。}\line{\loch\f3\cf2\ltrch此外,政府和半政府实体向合作伙伴提供了七项电子服务,以方便实体和基金之间的信息和交易交流,并支持阿布扎比政府发展服务的战略。\r第12行电子服务包括:预计将于2014年为养老金领取者、受益人和在职会员推出,这将提高所提供服务的水平。}\li0\ri0\sa0\sb0\fi0\ql\par} }
}span不是rtf标记,而是html,因此出现“Unrecognized structure”异常消息

尝试改用标记,或者首先通过html>RTF转换器放置html源代码

编辑:

好的,所以Span不是一个有效的rtf标记,但它是一个有效的wpf类,可以很好地嵌入到flowdocument中,所以我想它可以在这种情况下工作

但是您的代码有一个问题:span类似乎没有任何“dir”属性:


因此,我建议删除dir=“ltr”(如果我没有弄错的话,在本例中这是无用的)

textRange.Load(rtfMemoryStream,DataFormats.Rtf);原因是数据不是Rtf,而是它的HTML。它找不到Rtf样式的标记。但我已经有了另一行包含跨距的数据,并且工作正常