1

我在 SL 应用程序中有一个富文本。我想得到纯文本,所以我将带有格式的富文本转换为 xml,然后尝试摆脱所有格式标记。当我使用 SL4 运行应用程序时没问题。但不为 SL 5 起床。

这是丰富的格式数据:

<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Paragraph FontSize="20" FontFamily="Calibri" 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="20" FontFamily="Calibri" Foreground="#FFFF0000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal">Hello  ddd </Run>
  </Paragraph>
</Section>

我使用此代码删除标记(值高于输入数据):

 XDocument xmlRichText = XDocument.Parse(value.ToString());
  var q = from c in xmlRichText.Descendants(xamlPresentationNamespace + "Run")
          select (string)c.Attribute("Text");
  foreach (string text in q)
         result += text + " ";

如何使其适用于 SL 4 和 SL5?

4

1 回答 1

2

我没有看到任何名为“文本”的属性。如果您需要值“Hello ddd”,您可以这样做:

XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
var path = "...\file.xml";

using(var stream = File.Open(path, FileMode.Open))
{
    var q = XDocument.Load(stream).Descendants(ns + "Run").Select (d => d.Value);
    var result = string.Join(" ", q);
    Debug.WriteLine(result);
}

这适用于 LINQPad,但我尚未在 SL4 或 SL5 中进行测试。

于 2012-05-22T20:13:17.517 回答